# 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)};. Apr 7, 2014 at 7:42
• Not sure about what I just said, it's just a guess. 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. 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. Apr 7, 2014 at 7:54
• Oh of course! Yes, it works perfectly now. Thank you, @Manuel. 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}]

• Just look at my first comment to the question. That's probably the reason he says this answer is mathematically wrong :) In any case, I think this is what the OP asked for. The missing closing brace is from the copied code. If you remove the mark=none option and add +[ycomb] after every \addplot it looks exactly like that answer. I think may be that answer should be a comment to my answer. Dec 14, 2015 at 9:29
• Yes, I thought it should rather be here. Either another answer or a comment. I see you added the missing curly bracket in an edit today so I'm not sure what you mean about its being in the copied code. It was missing in your answer. I guess you mean that you copied it from the question without testing it ;). Indeed, I already tried the ycomb stuff, which is why I say it isn't at all clear from the other answer why this one is supposed to be mathematically incorrect. (That is, I don't understand the claim made there.) I agree the request to represent a discrete fn. as if continuous is odd.
• You should add the bracket here: samples at = {0,...,15}} and remove it at the end. It was present in the original question, you just added a { and used the already }. Oct 21, 2016 at 19:29