[Tex/LaTex] Plotting Normal distribution in pgfplots

pgfplots

I'm trying to reproduce the following diagram in TikZ and pgfplots.
enter image description here

My MWE with regenerated graph is below:

\documentclass{standalone}
\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:6, 
  samples=100,
  axis lines*=left, 
  xlabel=$x$,
  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
  ]

 \addplot [very thick,cyan!50!black] {gauss(3, 1)};
\draw [yshift=1.4cm, latex-latex](axis cs:2, 0) -- node [fill=white] {$0.683$} (axis cs:4, 0);
\draw [yshift=0.3cm, latex-latex](axis cs:1, 0) -- node [fill=white] {$0.954$} (axis cs:5, 0);


\end{axis}

\node[below] at (1, 0)  {$\mu - 2\sigma$}; 
\node[below] at (2, 0)  {$\mu - \sigma$}; 
\node[below] at (3, 0)  {$\mu$}; 


\end{tikzpicture}

\end{document}

enter image description here

Issues

  1. Nodes are not properly placed.
  2. How to remove y-axis?
  3. How to draw vertical dotted line?

Any help will be highly appreciated. Thanks

Best Answer

  1. As Qrrbrbirlbel said, move the nodes into the axis environment and use (axis cs:...). Alternatively, you could move the nodes into the axis environment and set disabledatascaling in the axis options. That way, you can keep your coordinates (this won't work if you have very large values in your plot, though).
  2. Use hide y axis to hide the y axis
  3. For the lines, I would redefine the gauss function to take a third parameter (the x value). Then you can use \pgfmathsetmacro to calculate the value of the distribution at the required x coordinate, and use that to draw the lines:

\documentclass[border=5mm]{standalone}
\usepackage{pgfplots}

\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, 
  xlabel=$x$,
  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\valueA{gauss(1,3,1)}
\pgfmathsetmacro\valueB{gauss(2,3,1)}
\draw [gray] (axis cs:1,0) -- (axis cs:1,\valueA)
    (axis cs:5,0) -- (axis cs:5,\valueA);
\draw [gray] (axis cs:2,0) -- (axis cs:2,\valueB)
    (axis cs:4,0) -- (axis cs:4,\valueB);
\draw [yshift=1.4cm, latex-latex](axis cs:2, 0) -- node [fill=white] {$0.683$} (axis cs:4, 0);
\draw [yshift=0.3cm, latex-latex](axis cs:1, 0) -- node [fill=white] {$0.954$} (axis cs:5, 0);

\node[below] at (axis cs:1, 0)  {$\mu - 2\sigma$}; 
\node[below] at (axis cs:2, 0)  {$\mu - \sigma$}; 
\node[below] at (axis cs:3, 0)  {$\mu$}; 
\end{axis}



\end{tikzpicture}

\end{document}