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.

Can anyone tell me how to plot a gaussian function/bell curve using TikZ/PGF? I'm basically looking to implement something like PSTricks's \psGauss command.

share|improve this question
    
great question and great solution, thank you! Some days ago I was searching for something like that but only found an example in the TikZ gallery which required gnuplot and did not work for me. –  Martin Apr 10 '11 at 11:27

1 Answer 1

up vote 45 down vote accepted

You can use pgfplots to plot the functions. There's no standard macro for it, but the function isn't too complicated and can be added as a pgfmath function (based on this answer: How do I use pgfmathdeclarefunction to create define a new pgf function?):

\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}[every axis plot post/.append style={
  mark=none,domain=-2:3,samples=50,smooth}, % All plots: from -2:2, 50 samples, smooth, no marks
  axis x line*=bottom, % no box around the plot, only x and y axis
  axis y line*=left, % the * suppresses the arrow tips
  enlargelimits=upper] % extend the axes a bit to the right and top
  \addplot {gauss(0,0.5)};
  \addplot {gauss(1,0.75)};
\end{axis}
\end{tikzpicture}
\end{document}

pgfplots gauss distribution


In my original answer, I had just declared a normal LaTeX function to the same end:

\documentclass{article}
\usepackage{pgfplots}
\begin{document}

\newcommand\gauss[2]{1/(#2*sqrt(2*pi))*exp(-((x-#1)^2)/(2*#2^2))} % Gauss function, parameters mu and sigma

\begin{tikzpicture}
\begin{axis}[every axis plot post/.append style={
  mark=none,domain=-2:3,samples=50,smooth}, % All plots: from -2:2, 50 samples, smooth, no marks
axis x line*=bottom, % no box around the plot, only x and y axis
axis y line*=left, % the * suppresses the arrow tips
enlargelimits=upper] % extend the axes a bit to the right and top
\addplot {\gauss{0}{0.5}};
\addplot {\gauss{1}{0.75}};
\end{axis}
\end{tikzpicture}
\end{document}
share|improve this answer
    
Exactly what I needed, thank you. No way I would have worked this out on my own. –  mike Feb 18 '11 at 5:22
1  
Quick fix on the equation.. a bit messy but the post eqn did not work for me.. \newcommand\gauss[2]{(1/(#2*(sqrt(2*pi))))*exp(-0.5*(((x-#1)/#2)^2))} % Gauss function, parameters mu and sigma –  user14907 May 23 '12 at 19:56
2  
Is there anything similar for Poisson distribution in PGF? Thanks in advance. –  user17586 Aug 14 '12 at 15:13
    
I get the Error Missing number treated as zero in line "\end{axis} –  Adam Aug 28 '12 at 11:09
    
@Adam: It compiles fine for me. What version of PGFPlots are you using? –  Jake Aug 28 '12 at 11:16

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.