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

6
  • 1
    you can start looking at Texample.net
    – Ignasi
    Feb 6, 2012 at 18:19
  • 9
    Show what you have tried so far in form of some code. Feb 6, 2012 at 18:21
  • 4
    You can use the pgfplots package for this and more
    – percusse
    Feb 6, 2012 at 18:37
  • 6
    Along with the other suggestion, perhaps bell curve gaussian function in tikz pgf can get you going. Then when you run into specific issues, feel free to post questions. It will be extremely helpful if you compose a fully compilable MWE that illustrates the specific problem. BTW, Welcome to TeX.SE. Feb 6, 2012 at 18:53
  • I definitely don't have the time to provide a solution but for those who do have the time, it would help if you could tell what your picture shouldlook line and how it depends on which (of your) input parameters.
    – user10274
    Feb 7, 2012 at 0:04

2 Answers 2

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}
6
  • 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 <return> for immediate help. ... l.26 \end{axis} Any suggestions? Feb 10, 2012 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, 2012 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. Feb 10, 2012 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? Sep 4, 2013 at 4:49
  • @JunLiu: The vertical lines are grid lines, the positions are set using xtick={4, 6.5}
    – Jake
    Sep 4, 2013 at 5:29
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}
3
  • 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 7, 2012 at 4: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 7, 2012 at 4: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 7, 2012 at 4:37

You must log in to answer this question.

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