我试图在 tikz 中绘制一个概率函数,但绘制的线与函数应有的样子相差甚远。我是 Latex 的新手,我不知道为什么这不起作用,除非 tikz 无法处理这个函数。这是我的代码:
\documentclass{article}
\usepackage{pgfplots}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[
declare function={Probability(\n,\k) = 1-((\n!)/((\n^(\k))*((\n-
\k)!)));}
]
\begin{axis}[
no markers, domain=0:15, samples=2500,
axis lines=left, xlabel=$k$, ylabel=$y$,
every axis y label/.style={at=(current axis.above origin),anchor=east},
every axis x label/.style={at=(current axis.right of origin),anchor=north},
height=10cm, width=15cm,
xtick={0,2,4,6,8,10,12,14},
ytick={0.1,0.2,0.3,0.4,0.5,.6,.7,.8,.9,1},
enlargelimits=false, clip=false, axis on top,
grid = major,
xmax=15
]
\addplot [very thin,cyan!20!black] {Probability(10,x)};
\end{axis}
\end{tikzpicture}
\end{document}
如果有人能给我指明正确的方向,我将不胜感激。谢谢
答案1
答案仅涉及一个pgfplots
方面。要绘制(垂直)线,您需要添加选项ycomb
:
\documentclass{article}
\usepackage{pgfplots}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[
declare function={p(\n,\k) = 1-((\n!)/((\n^(\k))*((\n-\k)!)));}
]
\begin{axis}[axis lines=middle,
xlabel=$k$, ylabel=$y$,
ylabel style={anchor=south},
xlabel style={anchor=west},
ymax=1.2, xmax=15.5,
domain=0:15, samples=150,
no markers
]
\addplot+ [ycomb] {p(2,x)}; % <----
\end{axis}
\end{tikzpicture}
\end{document}
附录: 具有正确概率函数设置的图像(变量应为整数)是:
\begin{tikzpicture}[
declare function={p(\n,\k) = 1-((\n!)/((\n^(\k))*((\n-\k)!)));}
]
\begin{axis}[axis lines=middle,
xlabel=$k$, ylabel=$y$,
ylabel style={anchor=south},
xlabel style={anchor=west},
ymax=1.1, xmax=32, % <-------
domain=0:30, samples=31,% <-------
% no markers
]
\addplot+ [ycomb] {p(2,x)}; % <-------
\end{axis}
\end{tikzpicture}