5

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]
  \addplot {poiss(1)};
  \addplot {poiss(2)};
  \addplot {poiss(3)};
\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:

poisson - 20 samples

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

poisson - 10 samples

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.

  • 1
    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)};. – Manuel Apr 7 '14 at 7:42
  • Not sure about what I just said, it's just a guess. – Manuel Apr 7 '14 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. – davly Apr 7 '14 at 7:52
  • 1
    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. – Manuel Apr 7 '14 at 7:54
  • Oh of course! Yes, it works perfectly now. Thank you, @Manuel. – davly Apr 7 '14 at 10:24
8

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}]
  \addplot {poiss(1)};
  \addplot {poiss(2)};
  \addplot {poiss(3)};
\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}]
  \addplot +[ycomb] {poiss(1)};
  \addplot +[ycomb] {poiss(4)};
  \addplot +[ycomb] {poiss(7)};
\end{axis}
\end{tikzpicture}
\end{figure}
\end{document}
| improve this answer | |
  • There seems to be a missing closing curly bracket somewhere. Also, according to this comment the plot is mathematically incorrect although the answer there isn't really clear why (and I'm not saying the comment is true - I have no idea). – cfr Dec 14 '15 at 2:46
  • 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. – Manuel Dec 14 '15 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. – cfr Dec 14 '15 at 22:57
  • 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 }. – coyotte508 Oct 21 '16 at 19:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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