# Poisson distribution with tikzpicture

I am new to 'drawing' in LaTeX with pgfplots, tikz, etc. I am trying to plot a Poisson distribution with varying means. Here is what I have tried (based on this answer)

\documentclass{article}
\usepackage{pgfplots}
\pgfmathdeclarefunction{poiss}{1}{%
\pgfmathparse{(#1^x)*exp(-#1)/(x!)}%
}
\begin{document}
\begin{figure}
\begin{tikzpicture}
\begin{axis}[every axis plot post/.append style={
mark=none,domain=0:20,samples=20},
axis x line*=bottom,
axis y line*=left,
enlargelimits=upper]
\end{axis}
\end{tikzpicture}
\end{figure}
\end{document}

What happens next is very strange to me; if I plot this with domain=0:20,samples=20, it looks like the following:

However, if I change domain=0:10,samples=10, it looks like the following:

You can see that the higher mean distributions are 'taller' than they should be, depending on the number of samples; the larger the number samples, the more correct the distributions look but obviously a graph going from 0 to 50 on the x-axis with nothing interesting after 10 is no good!

I'm wondering why is this happening, and how can I fix it? Forgive me if it's something painfully obvious. Thanks.

• The difference is that this is not a continous distribution, hence you can only evaluate at integer values: domain = 0:10, samples at = {1,...,10}. However, since it's discrete, I would delete the mark = none in your code, and call the functions with \addplot+[ycomb] {poiss(1)};. Commented Apr 7, 2014 at 7:42
• Not sure about what I just said, it's just a guess. Commented Apr 7, 2014 at 7:49
• Well it allows me to plot with different features but it doesn't fix the problem at hand; the traces are still the same. Commented Apr 7, 2014 at 7:52
• No, the traces are different. Remember that when you say samples=10 if you say samples=11 you get the exact solution as mine. Because you want to evaluate the funcition in eleven places (0,1,…,10). That's why sometimes I prefer to say explicitly where I want it to evaluate the function. Commented Apr 7, 2014 at 7:54
• Oh of course! Yes, it works perfectly now. Thank you, @Manuel. Commented Apr 7, 2014 at 10:24

Remember that you want to evaluate the funcition in eleven places (0,1,…,10), not ten. Anyway, I would use samples at = {0,...,10} to place the evaluating points at wish.

\documentclass{article}
\usepackage{pgfplots}
\pgfmathdeclarefunction{poiss}{1}{%
\pgfmathparse{(#1^x)*exp(-#1)/(x!)}%
}
\begin{document}
\begin{figure}
\begin{tikzpicture}
\begin{axis}[every axis plot post/.append style={
mark=none,domain=0:10,samples at = {0,...,10},
axis x line*=bottom,
axis y line*=left,
enlargelimits=upper}]
\end{axis}
\end{tikzpicture}
\end{figure}
\end{document}

As pointed out in the comment on the question and in another question, since this is a discrete distribution, it should really not be plotted as a line plot, but rather with the ycomb option instead:

\documentclass{article}
\usepackage{pgfplots}
\pgfmathdeclarefunction{poiss}{1}{%
\pgfmathparse{(#1^x)*exp(-#1)/(x!)}%
}
\begin{document}
\begin{figure}
\begin{tikzpicture}
\begin{axis}[every axis plot post/.append style={
samples at = {0,...,15},
axis x line*=bottom,
axis y line*=left,
enlargelimits=upper}]