[Tex/LaTex] How to plot a normal curve with proper y-values

pgfplotstikz-pgf

Follow-up question to Plotting bell shaped curve in TikZ-PGF

I'm new to LaTeX, and I'm typesetting a document for my stats class, and trying to use LaTeX to produce a normal probability distribution. I modified the code shown in Jake's answer to the question I referenced here, and got the following:

\documentclass{article}
\usepackage{pgfplots}
\pgfmathdeclarefunction{gauss}{2}{% normal distribution where #1 = mu and #2 = sigma
  \pgfmathparse{1/(#2*sqrt(2*pi))*exp(-((x-#1)^2)/(2*#2^2))}%
}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
  no markers, domain=2.1:2.3, samples=100,
  axis lines*=left,
  height=5cm, width=12cm,
  xtick={2.12, 2.14, 2.16, 2.18, 2.2, 2.22, 2.24, 2.26, 2.28}, ytick={0.5, 1.0},
  enlargelimits=false, clip=false, axis on top,
  ]
\addplot{gauss(2.2,0.0179)};
\end{axis}
\end{tikzpicture}
\end{document}

The problem is that when I compile, the y-axis values are nowhere near where they should be:
code output

How do I fix that?

I'm wondering if it's a system problem, because the output shown in Jake's answer on Bell Curve/Gaussian Function/Normal Distribution in TikZ/PGF has appropriate y-axis values and I don't see why it should be any different.

Best Answer

You are not getting values between 0 and 1 for the y-axis due to the multiplicative factor 1/(#2*sqrt(2*pi)) in the expression for the funtion. Using

ytick={0.5, 1.0}

gives values that are too small, for the actual range that takes values from 0 to approx. 20; . Use an appropriate range of values in the y-axis:

\documentclass{article}
\usepackage{pgfplots}
%\pgfplotsset{compat=1.10}

\pgfmathdeclarefunction{gauss}{2}{% normal distribution where #1 = mu and #2 = sigma
  \pgfmathparse{1/(#2*sqrt(2*pi))*exp(-((x-#1)^2)/(2*#2^2))}%
}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
  no markers, domain=2.1:2.3, samples=100,smooth,
  axis lines*=left,
  height=5cm, width=12cm,
  xtick={2.12, 2.14, 2.16, 2.18, 2.2, 2.22, 2.24, 2.26, 2.28}, 
  ytick={4.0,8.0,...,20.0},
  enlargelimits=upper, clip=false, axis on top,
  ]
\addplot{gauss(2.2,0.0179)};
\end{axis}
\end{tikzpicture}
\end{document}

enter image description here

Or, to get a proper range between 0 and 1, suppress the factor in the defining equation:

\documentclass{article}
\usepackage{pgfplots}
%\pgfplotsset{compat=1.10}

\pgfmathdeclarefunction{gauss}{2}{% normal distribution where #1 = mu and #2 = sigma
  \pgfmathparse{exp(-((x-#1)^2)/(2*#2^2))}%
}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
  no markers, domain=2.1:2.3, samples=100,smooth,
  axis lines*=left,
  height=5cm, width=12cm,
  xtick={2.12, 2.14, 2.16, 2.18, 2.2, 2.22, 2.24, 2.26, 2.28}, 
  enlargelimits=upper, clip=false, axis on top,
  ]
\addplot{gauss(2.2,0.0179)};
\end{axis}
\end{tikzpicture}
\end{document}

enter image description here

Related Question