有人能帮我在 Latex 中绘制均匀分布函数的 PDF 吗?我是这方面的新手,找不到任何有用的东西。谢谢!
答案1
如果有人有兴趣,这里是另一个带有符号坐标的解决方案。
\documentclass[10pt]{standalone}
\usepackage{mathtools}
\usepackage{tkz-euclide}
% arrow and line for 'tkzPointShowCoord'
\makeatletter
\tikzset{arrow coord style/.style={%
densely dashed,
\tkz@euc@linecolor,
%>=stealth',
%->,
}}
\tikzset{xcoord style/.style={%
\tkz@euc@labelcolor,
font=\normalsize,text height=1ex,
inner sep = 0pt,
outer sep = 0pt,
fill=\tkz@fillcolor,
below=6pt
}}
\tikzset{ycoord style/.style={%
\tkz@euc@labelcolor,
font=\normalsize,text height=1ex,
inner sep = 0pt,
outer sep = 0pt,
fill=\tkz@fillcolor,
left=6pt
}}
\makeatother
\begin{document}
\begin{tikzpicture}
\tkzInit[xmax=1,xstep=0.2,ymax=1,ystep=0.2]
\tkzDrawX[noticks,label={$x$}]
\tkzDrawY[noticks,label={$f_X(x)$}]
\tkzDefPoint(0.2,0.8){A}
\tkzDefPoint(0.8,0.8){B}
\tkzDefShiftPoint[A](-90:4){AB}
\tkzDefShiftPoint[B](-90:4){BB}
\tkzDefShiftPoint[AB](0:-0.6){ABL}
\tkzDefShiftPoint[BB](0:0.6){BBR}
\tkzPointShowCoord[xlabel=$a$,ylabel=$\frac{1}{b-a}$](A)
\tkzPointShowCoord[xlabel=$b$](B)
\tkzDrawSegments[color=cyan,thick](A,B AB,ABL BB,BBR)
\tkzDrawPoints[color=cyan,fill=cyan,size=6pt](A,B)
\tkzDrawPoints[color=cyan,fill=white,size=6pt](AB,BB)
\end{tikzpicture}
\end{document}
答案2
你可以使用 PGFPlots 来实现这一点。默认情况下,均匀分布的 PDF 不实现,但你可以使用以下方法轻松定义它:
declare function={unipdf(\x,\xl,\xu)= (\x>\xl)*(\x<\xu)*1/(\xu-\xl);}
你可以这样写unipdf(<variable>,<lower>,<upper>)
:
\documentclass{article}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}[
declare function={unipdf(\x,\xl,\xu)= (\x>\xl)*(\x<\xu)*1/(\xu-\xl);}
]
\begin{axis}[
samples=100,
const plot mark mid,
ymin=0,ymax=1
]
\addplot [very thick, orange] {unipdf(x,0,2)};
\addplot [very thick, cyan] {unipdf(x,-3,1)};
\end{axis}
\end{tikzpicture}
\end{document}
为了获得具有开/闭区间标记的适当的不连续函数,您可以使用该scatter/@pre marker code
功能来决定是否绘制标记:
\documentclass{article}
\usepackage{pgfplots}
\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
\ifdim\pgf@y<0pt
\tikzset{options/.style={mark=*,fill=white}}
\draw [densely dashed] (marker-|0,0) -- (0,0);
\draw plot [mark=*] coordinates {(marker-|0,0)};
\else
\tikzset{options/.style={mark=none}}
\fi
\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}[
declare function={unipdf(\x,\xl,\xu)= (\x>=\xl)*(\x<\xu)*1/(\xu-\xl);}
]
\begin{axis}[
samples=11,
jump mark left,
ymin=0,ymax=1,
xmin=-5, xmax=5,
every axis plot/.style={very thick},
discontinuous
]
\addplot [orange] {unipdf(x,0,2)};
\end{axis}
\end{tikzpicture}
\end{document}