# Filling in the area under a normal distribution curve [duplicate]

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}

-

## marked as duplicate by Ignasi, Claudio Fiandrino, Jesse, Andrew Swann, barbara beetonApr 25 '14 at 13:23

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 8 '14 at 18:27
Thanks for the info, I didn't know. I will try to do that in the future thanks! – dlae90 Mar 8 '14 at 19:35

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


before your \addplot should do it.

-

A PSTricks solution:

\documentclass{article}

\usepackage{pst-func}
\usepackage{expl3}

\ExplSyntaxOn
\cs_new_eq:NN \calc \fp_eval:n
\ExplSyntaxOff

% constants
\newcommand*\constantA{\calc{round(1/\Sigma,2)}}
\newcommand*\constantB{\calc{round(2*\Sigma^2,2)}}

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

% label
\newcommand*\LabelA[1]{%
\psline(#1,-0.02)(#1,0.02)
\uput[90](!#1 0.25 sub 0.25){\footnotesize $z = #1$}
\psline[linewidth = 0.02]{->}(!#1 0.25 sub 0.25)(#1,0)}
\newcommand*\LabelB[1]{%
\psline(#1,-0.02)(#1,0.02)
\uput[90](!#1 0.25 add 0.25){\footnotesize $z = #1$}
\psline[linewidth = 0.02]{->}(!#1 0.25 add 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(\calc{\Mue+1.3},\calc{\Gauss[\Mue,\Sigma]{\Mue}-0.1}){%
%    $(\mu,\sigma) = (\Mue,\Sigma)$}
\rput(\Mue,\calc{\Gauss[\Mue,\Sigma]{\Mue}+0.2}){%
$P(z) = {\displaystyle\frac{\constantA}{\sqrt{2\pi}}} \exp{\mkern -8mu}\left(-\frac{(z-\Mue)^{2}}{\constantB}\right)$}
\LabelA{\pointA}
\LabelB{\pointB}
\end{pspicture}

\end{document}


-
Thank you!!!! This helps!! – dlae90 Mar 10 '14 at 4:28

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);
\end{scope}


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);
\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}

-
Thank you! this is great! – dlae90 Mar 10 '14 at 4:28