# Plotting Multiple Normal distributions with pgfplots

Output 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

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} 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.

• I think it would be better to use the domain key instead of restrict x to domain. That way, you have less computational overhead, since only relevant values are calculated. – Jake May 18 '14 at 14:51
• @Jake agreed. I have added a note in the answer. Thank you. – user11232 May 18 '14 at 15:02
• (+1): Thank @HarishKumar for your answer. Much appreciated. – MYaseen208 May 18 '14 at 15:42

run with xelatex:

\documentclass[12pt,a4paper]{report}
\usepackage{pst-func}
\begin{document}

\psset{yunit=3}
\begin{pspicture}(-1,-1)(\linewidth,1.3)
\psGauss[linecolor=blue, linewidth=2pt]{-2}{2}
\psline[linestyle=dashed,linecolor=blue](0,-0.02)(*0 {sqrt(2/Pi)})
\uput[-90](0,0){\blue$\mu_1$}
\rput(4,0){%
\psGauss[linecolor=green, linewidth=2pt]{-2}{2}
\psline[linestyle=dashed,linecolor=green](0,-0.02)(*0 {sqrt(2/Pi)})
\uput[-90](0,0){\green$\mu_2$}}
\rput(8,0){
\psGauss[linecolor=red, linewidth=2pt]{-2}{2}
\psline[linestyle=dashed,linecolor=red](0,-0.02)(*0 {sqrt(2/Pi)})
\uput[-90](0,0){\red$\mu_3$}}
\end{pspicture}

\end{document} Hint from @HarishKumar \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:-5.5, 0) -- (axis cs:5.5, 0);

% 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:3.25, 0) -- (axis cs:14.5, 0);

% 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:12.5, 0) -- (axis cs:23.5, 0);

\end{axis}

\end{tikzpicture}

\end{document}