3

I want to plot a bar diagram of a binomial distribution where some bars have a darker color than the others (in my example: the bars from 13 to 19). If I use the following code, then the bars at k=13 and k=20 are missing. If I try to change the boundaries (e.g. from 12 to 12.5 or even 13) it's either ignored or produces a bar with the wrong height.

I also tried overlapping two plots which did not work, and in addition the ticks on the x-axis were then no longer in the middle of the bars but on the left.

Any suggestions would be very much appreciated. Thanks!

\documentclass{standalone}

\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat = newest}  

\begin{document}

\begin{tikzpicture}[declare function={binom(\k,\n,\p)=\n!/(\k!*(\n-\k)!)*\p^\k*(1-\p)^(\n-\k);}]
\begin{axis}[
    tick style={color=black},
    axis lines=middle,
    xmin=0,xmax=42,
    ymin=0,ymax=0.19,
    width=12cm,
    height=5cm,
    xlabel=$k$,
    ylabel={$B_{40;\,0,4}(k)$},
    samples at={0,...,35},
    ybar=0pt, bar width=1
]
\addplot [fill=black, fill opacity=0.3,restrict x to domain=0:12] {binom(x,40,0.4)}; 
\addplot [fill=black, fill opacity=0.6,restrict x to domain=13:19] {binom(x,40,0.4)}; 
\addplot [fill=black, fill opacity=0.3,restrict x to domain=20:40] {binom(x,40,0.4)}; 
\end{axis}
\end{tikzpicture}

\end{document}

Binomialdistribution with missing bars

  • Thanks! Very helpful forum. Got a nice, polite and helpful answer within hours. – Tobi Jul 6 '18 at 12:54
6

You have to add bar shift=0pt after option ybar:

\documentclass{standalone}
\usepackage{pgfplots}% loads also tikz
\pgfplotsset{compat = 1.16}
\begin{document}
\begin{tikzpicture}[declare function={binom(\k,\n,\p)=\n!/(\k!*(\n-\k)!)*\p^\k*(1-\p)^(\n-\k);}]
\begin{axis}[
    tick style={color=black},
    axis lines=middle,
    xmin=0,xmax=42,
    ymin=0,ymax=0.19,
    width=12cm,
    height=5cm,
    xlabel=$k$,
    ylabel={$B_{40;\,0,4}(k)$},
    samples at={0,...,35},
    ybar,bar width=1,
    bar shift=0pt% <- added: **after** ybar!!
]
\addplot [fill=black, fill opacity=0.3,restrict x to domain=0:12] {binom(x,40,0.4)}; 
\addplot [fill=black, fill opacity=0.6,restrict x to domain=13:19] {binom(x,40,0.4)};
\addplot [fill=black, fill opacity=0.3,restrict x to domain=20:40] {binom(x,40,0.4)}; 
\end{axis}
\end{tikzpicture}
\end{document}

Result: enter image description here


Explanation why bar shift=0pt is needed

There are three plots in your MWE. By default the bars of the first plot are shifted to the left and the bars of the third plat are shift to the right. So all three plots are visible. The value of key ybar sets the space between the bars of the first and the second plot and between the second and the third plot. You are using ybar=0pt, so the plots are next to each other.

\documentclass{standalone}
\usepackage{pgfplots}% loads also tikz
\pgfplotsset{compat = 1.16}
\begin{document}
\begin{tikzpicture}[declare function={binom(\k,\n,\p)=\n!/(\k!*(\n-\k)!)*\p^\k*(1-\p)^(\n-\k);}]
\begin{axis}[
    tick style={color=black},
    axis lines=middle,
    xmin=0,xmax=42,
    ymin=0,ymax=0.19,
    width=12cm,
    height=5cm,
    xlabel=$k$,
    ylabel={$B_{40;\,0,4}(k)$},
    samples at={0,...,35},
    ybar=0pt,bar width=1,
]
\addplot [fill=green, fill opacity=0.3,samples at=15] {binom(x,40,0.4)}; 
\addplot [fill=yellow, fill opacity=0.6,samples at=15] {binom(x,40,0.4)};
\addplot [fill=blue, fill opacity=0.3,samples at=15] {binom(x,40,0.4)}; 
\end{axis}
\end{tikzpicture}
\end{document}

Result:

enter image description here

If there are only two plots, the first plot is shifted to the left and the third is shifted to the right:

Comment the third plot in the example above to get:

enter image description here

If you add bar shift=0pt (after ybar!!), then the bars do not shift and so they overlap each other:

\documentclass{standalone}
\usepackage{pgfplots}% loads also tikz
\pgfplotsset{compat = 1.16}
\begin{document}
\begin{tikzpicture}[declare function={binom(\k,\n,\p)=\n!/(\k!*(\n-\k)!)*\p^\k*(1-\p)^(\n-\k);}]
\begin{axis}[
    tick style={color=black},
    axis lines=middle,
    xmin=0,xmax=42,
    ymin=0,ymax=0.19,
    width=12cm,
    height=5cm,
    xlabel=$k$,
    ylabel={$B_{40;\,0,4}(k)$},
    samples at={0,...,35},
    ybar=0pt,bar width=1,
    bar shift=0pt% <- added: **after** ybar!!
]
\addplot [fill=green, fill opacity=0.3,samples at=15] {binom(x,40,0.4)}; 
\addplot [fill=yellow, fill opacity=0.6,samples at=15] {binom(x,40,0.4)};
%\addplot [fill=blue, fill opacity=0.3,samples at=15] {binom(x,40,0.4)}; 
\end{axis}
\end{tikzpicture}
\end{document}

Result:

enter image description here

Or with all three plots:

enter image description here

| improve this answer | |
  • Very nice! +1 Perhaps even simpler if you say samples at={0,...,12} etc. – user121799 Jul 5 '18 at 3:36
  • Very nice indeed; thank you so much 'esdd' ! Could you please also tell me why the x-ticks get shifted to the left of the bars, when I plot two overlapping plots? – Tobi Jul 6 '18 at 6:59
  • @Tobi See my updated answer: the ticks get not shifted but the bars ;-) – esdd Jul 6 '18 at 10:20
  • @esdd & marmot: Thank you so much for the quick and very helpful comments & updates! Works perfectly. – Tobi Jul 6 '18 at 11:34
  • @Tobi If the answer helps you, then you can upvote and maybe accept the answer. – esdd Jul 6 '18 at 12:05

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.