1

I am trying to plot the theoretical binomial distribution with pgfplots but don't get the desired output:

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


\begin{python}%
import numpy as np
from scipy import special
N,p=150,.5
def f(n):
    return special.binom(N,n)*p**n*(1-p)**(N-n)
s = map(f,range(1,N))
np.savetxt("bernoulli.dat",s,fmt='%0.5f')
\end{python}%


\begin{tikzpicture}
  \begin{axis}[]
    \addplot[%
    hist={density,bins=150,data min=0,data max=150},
    fill=green,
    ]%
    table [y index=0] {bernoulli.dat};
  \end{axis}
\end{tikzpicture}

\end{document}

Output:

output

Any idea what's wrong with this?

Edit

You have to compile this one with pdflatex and --shell-escape.

bernoulli.dat looks like this:

0.00000
0.00000
0.00000
0.00000
0.00000
0.00000
0.00000
0.00000
0.00000
0.00000
0.00000
0.00000
0.00000
0.00000
0.00000
0.00000
0.00000
0.00000
0.00000
0.00000
0.00000
0.00000
0.00000
0.00000
0.00000
0.00000
0.00000
0.00000
0.00000
0.00000
0.00000
0.00000
0.00000
0.00000
0.00000
0.00000
0.00000
0.00000
0.00000
0.00000
0.00000
0.00000
0.00000
0.00000
0.00000
0.00000
0.00000
0.00000
0.00001
0.00001
0.00003
0.00005
0.00010
0.00017
0.00031
0.00052
0.00085
0.00137
0.00213
0.00324
0.00478
0.00686
0.00958
0.01302
0.01723
0.02219
0.02782
0.03396
0.04035
0.04669
0.05261
0.05773
0.06168
0.06418
0.06504
0.06418
0.06168
0.05773
0.05261
0.04669
0.04035
0.03396
0.02782
0.02219
0.01723
0.01302
0.00958
0.00686
0.00478
0.00324
0.00213
0.00137
0.00085
0.00052
0.00031
0.00017
0.00010
0.00005
0.00003
0.00001
0.00001
0.00000
0.00000
0.00000
0.00000
0.00000
0.00000
0.00000
0.00000
0.00000
0.00000
0.00000
0.00000
0.00000
0.00000
0.00000
0.00000
0.00000
0.00000
0.00000
0.00000
0.00000
0.00000
0.00000
0.00000
0.00000
0.00000
0.00000
0.00000
0.00000
0.00000
0.00000
0.00000
0.00000
0.00000
0.00000
0.00000
0.00000
0.00000
0.00000
0.00000
0.00000
0.00000
0.00000
0.00000
0.00000
0.00000
0.00000
0.00000
  • 1
    If bernoulli.dat has the right contents then this is a tikz/pgfplots question that someone (not me) should be able to answer here. Perhaps add the contents of bernoulli.dat to your question. – Ethan Bolker Jan 26 '14 at 17:22
  • I've always put both x and y (comma delimited) into a file to be read by table. – John Kormylo Jan 26 '14 at 17:54
  • @JohnKormylo I just tried your suggestion by replacing s = map(f,range(1,N)) by s = zip(range(1,N),map(f,range(1,N))) I get a list ouf tuples then I have to remove the [y index=0] part. However I get the same result. If I plot it without hist option I the desired curve but I need it as histogram. – Anna Jan 26 '14 at 18:01
  • @Anna I don't think that you want the hist option: you already have the distribution. I think you rather need something like const plot. – clemens Jan 26 '14 at 18:14
  • @cgnieder const plot works, but I want also the vertical lines as in a histogram, i.e. it should look exactly like a histogram. – Anna Jan 26 '14 at 18:34
3

Putting @John Kormylos und @cgnieders comments together I get the following solution using ybar interval instead of hist:

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


\begin{python}%
import numpy as np
from scipy import special
N,p=150,.5
def f(n):
    return special.binom(N,n)*p**n*(1-p)**(N-n)
s = zip(range(1,N),map(f,range(1,N)))
np.savetxt("bernoulli.dat",s,fmt='%0.5f')
\end{python}%


\begin{tikzpicture}
  \begin{axis}[width=10cm]
    \addplot[%
    ybar interval,
    fill=blue!30,
    draw opacity=0.5,
    line width=0.4pt,
    ]%
    table {bernoulli.dat};
  \end{axis}
\end{tikzpicture}

\end{document}

Output: output

2

Problem solved by Anna but you might be interested in knowing you can get the same results using sagetex package without the need for creating the data file along the way. Sage gives you Python plus extra commands helpful for math.

\documentclass{article}
\usepackage{sagetex}
\usepackage{pgfplots}
\pagestyle{empty}
\begin{document}
\begin{sagesilent}
p = .5
N = 140

output =""
output += r"\begin{tikzpicture}[scale=1.0]"
output += r"\begin{axis}[width=10cm]"
output += r"\addplot[ybar interval,fill=blue!30,draw opacity=0.5,line   width=0.4pt] coordinates{"
for i in range(0,N):
    output += r"(%d, %f) "%(i, binomial(N,i)*p^i*(1-p)^(N-i))

output += r"};"
output += r"\end{axis}"
output += r"\end{tikzpicture}"
\end{sagesilent}
\sagestr{output}
\end{document}

No data file needed to get this output running in Sagemath Cloud: enter image description here

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.