我尝试创建这些图表,但无法获取 x 轴上的小数位数
\pgfplotsset{compat=1.11}
\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,...,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}
答案1
您需要在samples at
键中定义间隔宽度。例如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}
编辑
我使用条宽为零的条形图在每个标记下添加了线条。
\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}
答案2
您的图表也可以绘制如下:
- 定义域:
domain=0:1
, - 定义样本数量,例如:
samples=100
, - 使用
ycomb
选项(图表将变得类似于问题中显示的), - 稍微重新设计轴选项:
\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}