声明 pgfplots 使用的函数

声明 pgfplots 使用的函数

我想在 pgfplots 中定义二项分布函数。我的 MWE

\documentclass{article}

\usepackage{pgfplots} 

\usetikzlibrary{arrows.meta}
\pgfplotsset{compat=1.15}

\begin{document}

\begin{tikzpicture}
[
  declare function = {
    binom(\n, \k) = \n! / \k! / (\n - \k)!;
    binompdf(\n, \p, \k) = binom(\n, \k) * \p^\k * (1 - \p)^(\n - \k);
    % binomcdf(\n, \p, \k) = sum(\i = 1 ... \k, binompdf(\n, \p, \i));
  }
]
\begin{axis}
[
  grid = none,
  tick style = {black},
  tick label style = {
    /pgf/number format/use comma,
   /pgf/number format/fixed,
   /pgf/number format/fixed zerofill},
  scaled ticks = false,
  % x axis
  xmin = 0, xmax = 0.01,
  axis x line = middle, x axis line style = -{Stealth},
  xlabel = $p$, xlabel style = {below},
  xtick = {0, 0.001, ..., 0.01}, xticklabels = {},
  extra x ticks = {0.001},
  xticklabel style = {/pgf/number format/precision = 3},
  % y axis
  ymin = 0, ymax = 1.1,
  axis y line = middle, y axis line style = -{Stealth},
  ylabel = $P(X \ge 10)$, ylabel style = {left},
  ytick = {0, 0.1, ..., 1.1}, yticklabels = {},
  extra y ticks = {0.1},
]
\addplot[domain = 0 : 0.01, samples = 1000] {0.8};
\addplot[domain = 0 : 0.01, samples = 1000] {1 - binompdf(2100, x, 0)};
\end{axis}
\end{tikzpicture}

\end{document}

binompdf()命令可以识别的变格\addplot。这是什么原因造成的?

笔记:最终目标是编写一个累积二项分布函数。目前,y 轴标签与绘制的函数不对应。

答案1

您不应该在函数定义中使用具有其他含义的变量,更重要的是,听取@egreg 的意见:避免不必要的空格。

在此处输入图片描述

\documentclass{article}

\usepackage{pgfplots} 

\usetikzlibrary{arrows.meta}
\pgfplotsset{compat=1.15}

\begin{document}

\begin{tikzpicture}
[
  declare function = {
    binom(\x,\y) = \x! / \y! / (\x - \y)!;
    },
 declare function ={
    binompdf(\x,\y,\z) = binom(\x, \z) * \y^\z * (1 - \y)^(\x - \z);
  }
]
\begin{axis}
[
  grid = none,
  tick style = {black},
  tick label style = {
    /pgf/number format/use comma,
   /pgf/number format/fixed,
   /pgf/number format/fixed zerofill},
  scaled ticks = false,
  % x axis
  xmin = 0, xmax = 0.01,
  axis x line = middle, x axis line style = -{Stealth},
  xlabel = $p$, xlabel style = {below},
  xtick = {0, 0.001, ..., 0.01}, xticklabels = {},
  extra x ticks = {0.001},
  xticklabel style = {/pgf/number format/precision = 3},
  % y axis
  ymin = 0, ymax = 1.1,
  axis y line = middle, y axis line style = -{Stealth},
  ylabel = $P(X \ge 10)$, ylabel style = {left},
  ytick = {0, 0.1, ..., 1.1}, yticklabels = {},
  extra y ticks = {0.1},
]
\addplot[domain = 0 : 0.01, samples = 100] {0.8};
\addplot[domain = 0 : 0.01, samples = 100] {1 - binompdf(2100, x, 0)};
\end{axis}
\end{tikzpicture}

\end{document}

相关内容