有人能解释一下为什么这个函数比诺姆(红色)对于 x=-6 和 x=+6 不起作用(与正确的图(灰色)比较)?
\documentclass{standalone}
\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=1.7}
\begin{document}
\begin{tikzpicture}
[declare function={binom(\N,\m)=\N! / (2^\N) / ((\N+\m)/2)! / ((\N-\m)/2)!;}]
\begin{axis}[ ymin=0,
ymax=0.4,
xmin=-6.5,
xmax=6.6,
axis lines=left,
x label style={at={(axis description cs:1,0.1)},anchor=north},
y label style={at={(axis description cs:-0.15,1)},rotate = -90, anchor=north},
samples at={-6,-4,...,6},
xtick={-6,-5,...,6},
yticklabel style={
/pgf/number format/fixed,
/pgf/number format/fixed zerofill,
/pgf/number format/precision=2},
ybar=0pt,
bar width=1,
bar shift=0pt]
\addplot [fill=red!25] {binom(6,x)};
\addplot [fill=gray!25,bar width=.5] plot coordinates {(-6,1/2^6)
(-4,{6!/2^6/5!/1!})
(-2,{6!/2^6/4!/2!})
(0,{6!/2^6/3!/3!})
(2,{6!/2^6/4!/2!})
(4,{6!/2^6/5!/1!})
(6,{6!/2^6/6!/0!})};
\end{axis}
\end{tikzpicture}
\end{document}
答案1
这是一个舍入误差:
\documentclass{standalone}
\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=1.7}
\begin{document}
\begin{tikzpicture}
\begin{axis}[ ymin=0,
axis lines=left,
samples at={0},
ybar=0pt,
bar width=1,
bar shift=0pt]
\addplot [fill=red!25] plot coordinates {(0,{6!/5!})};
\addplot [fill=gray!25,bar width=.5] plot coordinates {(0,{(12/2)!/5!})};
\end{axis}
\end{tikzpicture}
\end{document}
((\N+\m)/2)
您可以通过替换来解决这个问题round((\N+\m)/2)
(因此只需round
在第一个括号前面添加 - 并且对于-
一个也是一样)。