40

I want to create some bell shaped curves for demonstrating hypothesis testing in statistics. Can anybody help me drawing curves like shown in the following picture?

enter image description here

Thorsten
  • 12,872

2 Answers2

60

I would use pgfplots for this, as I find it easier to use than the "raw" TikZ plotting functions. To shade the area under a curve, supply [domain=<xmin>:<xmax>] to the \addplot function and add \closedcyle before the ; that ends the \addplot command.

\documentclass{article}
\usepackage{pgfplots}
\begin{document}

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

\begin{tikzpicture}
\begin{axis}[
  no markers, domain=0:10, samples=100,
  axis lines*=left, xlabel=$x$, ylabel=$y$,
  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={4,6.5}, ytick=\empty,
  enlargelimits=false, clip=false, axis on top,
  grid = major
  ]
  \addplot [fill=cyan!20, draw=none, domain=0:5.96] {gauss(6.5,1)} \closedcycle;
  \addplot [very thick,cyan!50!black] {gauss(4,1)};
  \addplot [very thick,cyan!50!black] {gauss(6.5,1)};


\draw [yshift=-0.6cm, latex-latex](axis cs:4,0) -- node [fill=white] {$1.96\sigma$} (axis cs:5.96,0);
\end{axis}

\end{tikzpicture}
\end{document}
Jake
  • 232,450
  • Thanks a lot. It looks really promising and easier to understand. However, I failed to compile the above code. It shows me a error message like this -

    code! Package pgfkeys Error: I do not know the key '/tikz/axis lines*' and I am goi ng to ignore it. Perhaps you misspelled it. See the pgfkeys package documentation for explanation. Type H for immediate help. ... l.26 \end{axis}

    Any suggestions?

    – kurtspiegel Feb 10 '12 at 16:22
  • You're probably using an outdated version of pgfplots (the current one is 1.5.1). You can either update (highly recommended, each new release of pgfplots brings a lot of new features and improvements) or try removing the offending lines (the axis lines* key is not necessary for the example in principle, it just tweaks the appearance of the axis lines). – Jake Feb 10 '12 at 16:26
  • That's correct. I had an outdated package. I have a Ubuntu 11.10 laptop and the pgfplot was installed under a debian package called texlive-pictures. The installed version was 1.2x. I have downloaded the latest version and replaced the old one and it's now working. Thanks for your support. – kurtspiegel Feb 10 '12 at 19:51
  • Which statement in the above code snippet plotted the two vertical lines going through the two points (4,0) and (6.5,0), respectively? – Novice User Sep 04 '13 at 04:49
  • @JunLiu: The vertical lines are grid lines, the positions are set using xtick={4, 6.5} – Jake Sep 04 '13 at 05:29
  • How would you change the "4" and "6.5" for alphabetic characters? – Weierstraß Ramirez Jan 23 '18 at 09:39
10

If you go here http://thetarzan.wordpress.com/2011/06/17/tikz-diagrams-for-economists-a-normal-pdf-with-shaded-area/ you will find a code snippet which I have used for plotting normal curves for exams and handouts. enter image description here

\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
% define normal distribution function 'normaltwo'
\def\normaltwo{\x,{4*1/exp(((\x-3)^2)/2)}}

% input y parameter
\def\y{4.4}

% this line calculates f(y)
\def\fy{4*1/exp(((\y-3)^2)/2)}

% Shade orange area underneath curve.
\fill [fill=orange!60] (2.6,0) -- plot[domain=0:4.4] (\normaltwo) -- ({\y},0) -- cycle;

% Draw and label normal distribution function
\draw[color=blue,domain=0:6] plot (\normaltwo) node[right] {};

% Add dashed line dropping down from normal.
\draw[dashed] ({\y},{\fy}) -- ({\y},0) node[below] {$y$};

% Optional: Add axis labels
\draw (-.2,2.5) node[left] {$f_Y(u)$};
\draw (3,-.5) node[below] {$u$};

% Optional: Add axes
\draw[->] (0,0) -- (6.2,0) node[right] {};
\draw[->] (0,0) -- (0,5) node[above] {};

\end{tikzpicture}
\end{document}
Peter Grill
  • 223,288
  • 5
    There is an issue in the way you have filled your curve. On the left hand side it lifts away from the x-axis. – qubyte Feb 07 '12 at 04:17
  • I appears my vision is not as good as it used to be. Had to go to 300% to see that. I put the referenced web snippet into a MWE but did not look close enough. – schumacher Feb 07 '12 at 04:34
  • I was only using the curve before myself. This is a very interesting way to fill under a curve. Change plot[domain=0:4.4] to plot[domain=0:2.0] to see what is happening. And to remove the white use plot[domain=-.5:4.4] But then the shading goes beyond the y-axis. – schumacher Feb 07 '12 at 04:37