5

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.

2
  • Or are some points supposed to have a probability density of 20? Commented Mar 26, 2014 at 3:36
  • 1
    The integral of the probability density function has to be 1. For distributions where the mass is concentrated over a small domain (like in your question) the values of the PDF itself can exceed 1.
    – Jake
    Commented Mar 26, 2014 at 5:36

1 Answer 1

3

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

4
  • But as a normal distribution, the values shouldn't be higher than 1. Maybe it's the equation? Commented Mar 26, 2014 at 2:54
  • @dejongbrent sure; it's the equation. Commented Mar 26, 2014 at 2:54
  • @dejongbrent Please see my updated answer. Commented Mar 26, 2014 at 2:56
  • After some thought, I've come to the conclusion that the original equation was correct, but I wasn't ready for the values it gave. Thanks for your help! Commented Mar 26, 2014 at 3:45

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .