这是离散随机变量的累积分布函数的代码。
\begin{axis}[
clip=false,
jump mark left,
ymin=0,ymax=1,
xmin=0, xmax=6,
every axis plot/.style={very thick},
discontinuous,
table/create on use/cumulative distribution/.style={
create col/expr={\pgfmathaccuma + \thisrow{f(x)}}
}
]
\addplot [red] table [y=cumulative distribution]{
x f(x)
0 0
1 1/15
2 2/15
3 1/5
4 4/15
5 1/3
6 0
};
\end{axis}
有人能帮忙提供代码吗?
谢谢你们的建议。这实际上是我第一次使用 StackExchange,因此可能不会遵循一些规则。
代码来自@Jake:tex.stackexchange.com/a/198397/38080。感谢 Jake 的分享。
答案1
discontinuous/.style
与给出的代码相比tex.stackexchange.com/a/198397,你必须改变路线
\tikzset{options/.style={mark=*,fill=white}}
到
\tikzset{options/.style={mark=*}}
和线
\draw plot [mark=*] coordinates {(marker-|0,0)};
到
\draw plot [mark=*,mark options={fill=white}] coordinates {(marker-|0,0)};
哪一个版本是正确的取决于函数是使用 < 还是 <= 定义的。
\documentclass{standalone}
\usepackage{pgfplots, pgfplotstable}
\pgfplotsset{compat=1.17}
\makeatletter
\long\def\ifnodedefined#1#2#3{%
\@ifundefined{pgf@sh@ns@#1}{#3}{#2}%
}
\pgfplotsset{
discontinuous/.style={
scatter,
scatter/@pre marker code/.code={
\ifnodedefined{marker}{
\pgfpointdiff{\pgfpointanchor{marker}{center}}%
{\pgfpoint{0}{0}}%
\ifdim\pgf@y>0pt
\tikzset{options/.style={mark=*}}
\draw [densely dashed] (marker-|0,0) -- (0,0);
\draw plot [mark=*,mark options={fill=white}] coordinates {(marker-|0,0)};
\else
\tikzset{options/.style={mark=none}}
\fi
}{
\tikzset{options/.style={mark=none}}
}
\coordinate (marker) at (0,0);
\begin{scope}[options]
},
scatter/@post marker code/.code={\end{scope}}
}
}
\makeatother
\begin{document}
\begin{tikzpicture}
\begin{axis}[
clip=false,
jump mark left,
ymin=0,ymax=1,
xmin=0, xmax=5,
every axis plot/.style={very thick},
discontinuous,
table/create on use/cumulative distribution/.style={
create col/expr={\pgfmathaccuma + \thisrow{f(x)}}
}
]
\addplot [red] table [y=cumulative distribution]{
x f(x)
0 1/15
1 2/15
2 1/5
3 4/15
4 1/3
5 0
};
\end{axis}
\end{tikzpicture}
\end{document}