Binomial distribution with decimal places in x axis?
You need to define the interval width in the samples at
key.
E.g. samples at={0.0,0.01,...,1.0}
.
\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.11}
\begin{document}
\begin{tikzpicture}[
declare function={binom(\k,\n,\p)=\n!/(\k!*(\n-\k)!)*\p^\k*(1-\p)^(\n-\k);}
]
\begin{axis}[
width=\textwidth,
height=\axisdefaultheight,
samples at={0.0,0.01,...,1.0},
yticklabel style={
/pgf/number format/fixed,
/pgf/number format/fixed zerofill,
/pgf/number format/precision=1
}
]
\addplot [only marks, orange] {binom(11,30,x)};
\end{axis}
\end{tikzpicture}
\end{document}
Edit
I added the lines under each marker using a bar chart with bars of zero width.
\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.11}
\begin{document}
\begin{tikzpicture}[
declare function={binom(\k,\n,\p)=\n!/(\k!*(\n-\k)!)*\p^\k*(1-\p)^(\n-\k);}
]
\begin{axis}[
width = \textwidth,
height = \axisdefaultheight,
title style = {align = center},
title = {Multiple Binomial Distributions \\ for $k=11$, $n=30$},
xlabel = {$p$},
ylabel = {probability of $k$ given $p$},
samples at = {0.0,0.01,...,1.0},
tick pos = lower,
tick align = outside,
every tick/.style = {black},
yticklabel style={
/pgf/number format/fixed,
/pgf/number format/fixed zerofill,
/pgf/number format/precision=1
}
]
\addplot [
ybar,
bar width = 0pt,
mark = *,
mark options = {
blue
},
mark size = 1pt,
red
]
{binom(11,30,x)};
\end{axis}
\end{tikzpicture}
\end{document}
Your diagram can also be drawn as follows:
- define domain:
domain=0:1
, - define number of samples, for example:
samples=100
, - use
ycomb
option (diagram will become similar to showed in question}, - slightly restyle axis options:
\documentclass[margin=2mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.17}
\begin{document}
\begin{tikzpicture}[%
declare function={binom(\k,\n,\p)=\n!/(\k!*(\n-\k)!)*\p^\k*(1-\p)^(\n-\k);}
]
\begin{axis}[
width=\textwidth, height=\axisdefaultheight,
font=\sffamily,
ylabel={probability of $k$ given $p$},
xlabel={$p$},
xmax=1,
enlarge x limits=0.05,
tick label style={/pgf/number format/assume math mode=true,
font=\footnotesize\sffamily},
yticklabel style={/pgf/number format/.cd, fixed,
fixed zerofill, precision=2},
domain=0:1,samples=100,
mark options={scale=0.75, blue},
]
\addplot +[ycomb,draw=orange] {binom(11,30,x)};
\end{axis}
\end{tikzpicture}
\end{document}