[Tex/LaTex] Plotting Multiple Normal distributions with pgfplots

pgfplotstikz-pgf

Output

enter image description here

MWE

\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=-7.5:25.5
  , samples=100
  , ymin=0
  , axis lines*=left
  , xlabel= 
   , 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=20cm
  , xtick=\empty
  , ytick=\empty
  , enlargelimits=false
  , clip=false
  , axis on top
  , grid = major
  , hide y axis
  , hide x axis
  ]



%\draw [help lines] (axis cs:-3.5, -0.4) grid (axis cs:3.5, 0.5);

% Normal Distribution 1
\addplot[blue, ultra thick] {gauss(x, 0, 1.75)};
\pgfmathsetmacro\valueA{gauss(0, 0, 1.75)}
\draw [dashed, thick, blue] (axis cs:0, 0) -- (axis cs:0, \valueA);
\node[below] at (axis cs:0, -0.02)  {\Large \textcolor{blue}{$\mu_{1}$}}; 
\draw[thick, blue] (axis cs:-0.0, -0.01) -- (axis cs:0.0, 0.01);

% Normal Distribution 2
\addplot[green, ultra thick] {gauss(x, 9, 1.75)};
\draw [dashed, thick, green] (axis cs:9, 0) -- (axis cs:9, \valueA);
\node[below] at (axis cs:9, -0.02)  {\Large \textcolor{green}{$\mu_{2}$}}; 
\draw[thick, green] (axis cs:9, -0.01) -- (axis cs:9, 0.01);

% Normal Distribution 3
\addplot[red, ultra thick] {gauss(x, 18, 1.75)};
\draw [dashed, thick, red] (axis cs:18, 0) -- (axis cs:18, \valueA);
\node[below] at (axis cs:18, -0.02)  {\Large \textcolor{red}{$\mu_{3}$}}; 
\draw[thick, red] (axis cs:18, -0.01) -- (axis cs:18, 0.01);

\end{axis}


\end{tikzpicture}

\end{document}

Questions

You can see that all three distributions cover the whole dimension (See x-axis line). I wonder how can I restrict the distributions to not across their dimensions? Any help will be highly appreciated. Thanks

Best Answer

You can use restrict x to domain key:

\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=-7.5:25.5
  , samples=100
  , ymin=0
  , axis lines*=left
  , xlabel=
   , 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=20cm
  , xtick=\empty
  , ytick=\empty
  , enlargelimits=false
  , clip=false
  , axis on top
  , grid = major
  , hide y axis
  , hide x axis
  ]



%\draw [help lines] (axis cs:-3.5, -0.4) grid (axis cs:3.5, 0.5);

% Normal Distribution 1
\addplot[blue, ultra thick,restrict x to domain=-6:6] {gauss(x, 0, 1.75)};
\pgfmathsetmacro\valueA{gauss(0, 0, 1.75)}
\draw [dashed, thick, blue] (axis cs:0, 0) -- (axis cs:0, \valueA);
\node[below] at (axis cs:0, -0.02)  {\Large \textcolor{blue}{$\mu_{1}$}};
\draw[thick, blue] (axis cs:-0.0, -0.01) -- (axis cs:0.0, 0.01);

% Normal Distribution 2
\addplot[green, ultra thick,restrict x to domain=3:15] {gauss(x, 9, 1.75)};
\draw [dashed, thick, green] (axis cs:9, 0) -- (axis cs:9, \valueA);
\node[below] at (axis cs:9, -0.02)  {\Large \textcolor{green}{$\mu_{2}$}};
\draw[thick, green] (axis cs:9, -0.01) -- (axis cs:9, 0.01);

% Normal Distribution 3
\addplot[red, ultra thick,restrict x to domain=12:24] {gauss(x, 18, 1.75)};
\draw [dashed, thick, red] (axis cs:18, 0) -- (axis cs:18, \valueA);
\node[below] at (axis cs:18, -0.02)  {\Large \textcolor{red}{$\mu_{3}$}};
\draw[thick, red] (axis cs:18, -0.01) -- (axis cs:18, 0.01);

\end{axis}


\end{tikzpicture}

\end{document}

enter image description here

Use symmetric suitable values for xmin and xmax around the maximum point.

As Jake notes, it is better to use domain (for ex. domain=3:15) key instead that will reduce the computational load.