13

enter image description here

I'm trying to fill the area between the two $z$-scores but I'm not having any luck. Could someone please help me with this?

\begin{tikzpicture}
\begin{axis}[
  no markers, 
  domain=0:6, 
  samples=100,
  ymin=0,
  axis lines*=left, 
  every axis y label/.style={at=(current axis.above origin),anchor=south},
  every axis x label/.style={at=(current axis.right of origin),anchor=west},
  height=5cm, 
  width=12cm,
  xtick=\empty, 
  ytick=\empty,
  enlargelimits=false, 
  clip=false, 
  axis on top,
  grid = major,
  hide y axis
  ]
 \addplot [very thick,cyan!50!black] {gauss(x, 3, 1)};

\pgfmathsetmacro\valueB{gauss(1,1,7)}
\pgfmathsetmacro\valueA{gauss(1,1,1.7)}
\pgfmathsetmacro\valueC{gauss(1,1,1)}

\draw [very thick, red]  (axis cs:1,0) -- (axis cs:1,\valueB);
\draw [very thick, red]  (axis cs:2,0) -- (axis cs:2,\valueA);
\draw [very thick, red]  (axis cs:3,0) -- (axis cs:3,\valueC);

\node[below] at (axis cs:3, 0)  {$\mu$}; 
\node[below] at (axis cs: 2, 0) {$z=-1.8$};
\node[below] at (axis cs: 1,0) {$z=-2.97$};
\end{axis}
\end{tikzpicture}
dlae90
  • 131
  • 1
    Welcome to TeX.SE. It would be helpful if turned your code into a fully compilable MWE including \documentclass. While solving problems can be fun, setting them up is not. Then, those trying to help can simply cut and paste your MWE and get started on solving the problem. – Peter Grill Mar 08 '14 at 18:27
  • Thanks for the info, I didn't know. I will try to do that in the future thanks! – dlae90 Mar 08 '14 at 19:35

3 Answers3

17

Adding

\addplot [draw=none, fill=yellow!25, domain=1:2] {gauss(x, 3, 1)} \closedcycle;

before your \addplot should do it.

enter image description here

Peter Grill
  • 223,288
7

A PSTricks solution:

\documentclass{article}

\usepackage{pst-func}
\usepackage{xfp}

% constants
\newcommand*\constA{\fpeval{round(1/\Sigma,2)}}
\newcommand*\constB{\fpeval{round(2*\Sigma^2,2)}}

% function
\def\Gauss[#1,#2]#3{\fpeval{1/(sqrt(2*pi)*#2)*exp(-(#3-#1)^2/(2*#2^2))}}

% labels
\newcommand*\LabelA[1]{%
  \psline(#1,-0.02)(#1,0.02)
  \uput[90](\fpeval{#1-0.25},0.25){\footnotesize $z = #1$}
  \psline[linewidth = 0.02]{->}(\fpeval{#1-0.25},0.25)(#1,0)}
\newcommand*\LabelB[1]{%
  \psline(#1,-0.02)(#1,0.02)
  \uput[90](\fpeval{#1+0.25},0.25){\footnotesize $z = #1$}
  \psline[linewidth = 0.02]{->}(\fpeval{#1+0.25},0.25)(#1,0)}

% settings
\psset{
  xunit = 2,
  yunit = 5
}

% parameters
\def\Mue{2.5}
\def\Sigma{0.44}
\def\pointA{1.5}
\def\pointB{2.2}


\begin{document}

\begin{pspicture}(-0.35,-0.1)(5.05,1.2)
  \pscustom[linestyle = none, fillstyle = solid, fillcolor = red!80]{%
    \psline(\pointA,0)(\pointA,\Gauss[\Mue,\Sigma]{\pointA})
    \psGauss[mue = \Mue, sigma = \Sigma]{\pointA}{\pointB}
    \psline(\pointB,\Gauss[\Mue,\Sigma]{\pointB})(\pointB,0)}
  \psaxes[Dx = 0.5, Dy = 0.2]{->}(0,0)(0,0)(4.9,1.1)[$z$,0][$P(z)$,90]
  \psline[linecolor = red!80](\Mue,\Gauss[\Mue,\Sigma]{\Mue})(\Mue,0)
  \psGauss[mue = \Mue, sigma = \Sigma, linecolor = blue!80]{0.25}{4.75}
%  \rput(\fpeval{\Mue+1.3},\fpeval{\Gauss[\Mue,\Sigma]{\Mue}-0.1}){%
%    $(\mu,\sigma) = (\Mue,\Sigma)$}
  \rput(\Mue,\fpeval{\Gauss[\Mue,\Sigma]{\Mue}+0.2}){%
    $P(z) = {\displaystyle\frac{\constA}{\sqrt{2\pi}}}
             \exp{\mkern -8mu}\left(-\frac{(z-\Mue)^{2}}{\constB}\right)$}
  \LabelA{\pointA}
  \LabelB{\pointB}
\end{pspicture}

\end{document}

output

5

Another alternative is the use of scope environment via \clip command.

\begin{scope}[yshift=-\pgflinewidth]
\clip (axis cs:1,0) rectangle (axis cs:2,0.24);
\addplot [draw=none,fill=blue] {gauss(x, 3, 1)};
\end{scope}

enter image description here

Code

\documentclass[border=2cm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.8}
\begin{document}

\pgfmathdeclarefunction{gauss}{3}{%
\pgfmathparse{1/(#3*sqrt(2*pi))*exp(-((#1-#2)^2)/(2*#3^2))}%
}

\begin{tikzpicture}
\begin{axis}[
  no markers, 
  domain=0:6, 
  samples=100,
  ymin=0,
  axis lines*=left, 
  every axis y label/.style={at=(current axis.above origin),anchor=south},
  every axis x label/.style={at=(current axis.right of origin),anchor=west},
  height=5cm, 
  width=12cm,
  xtick=\empty, 
  ytick=\empty,
  enlargelimits=false, 
  clip=false, 
  axis on top,
  grid = major,
  hide y axis
  ]

\begin{scope}[yshift=-\pgflinewidth]
\clip (axis cs:1,0) rectangle (axis cs:2,0.24);
\addplot [draw=none,fill=blue] {gauss(x, 3, 1)};
\end{scope}

\addplot [very thick,cyan!50!black] {gauss(x, 3, 1)};
\pgfmathsetmacro\valueB{gauss(1,1,7)}
\pgfmathsetmacro\valueA{gauss(1,1,1.65)}
\pgfmathsetmacro\valueC{gauss(1,1,1)}

\draw [very thick, red]  (axis cs:1,0) -- (axis cs:1,\valueB);
\draw [very thick, red]  (axis cs:2,0) -- (axis cs:2,\valueA);
\draw [very thick, red]  (axis cs:3,0) -- (axis cs:3,\valueC);

\node[below] at (axis cs:3,0)  {$\mu$}; 
\node[below] at (axis cs:2,0) {$z=-1.8$};
\node[below] at (axis cs:1,0) {$z=-2.97$};
\end{axis}

\end{tikzpicture}
\end{document}
Jesse
  • 29,686