Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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

share|improve this question

3 Answers 3

up vote 9 down vote accepted

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.

share|improve this answer
2  
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 at 14:51
    
@Jake agreed. I have added a note in the answer. Thank you. –  Harish Kumar May 18 at 15:02
    
(+1): Thank @HarishKumar for your answer. Much appreciated. –  MYaseen208 May 18 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}

enter image description here

share|improve this answer

Hint from @HarishKumar

enter image description here

\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}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.