![概率密度函数图](https://linux22.com/image/360634/%E6%A6%82%E7%8E%87%E5%AF%86%E5%BA%A6%E5%87%BD%E6%95%B0%E5%9B%BE.png)
我想制作那样的 PDF 图表。
目前,我在这里。有人能帮我进一步完成图表吗?提前谢谢。
\documentclass{article}
\usepackage{pgfplots}
\begin{document}
\pgfmathdeclarefunction{gauss}{2}{%
\pgfmathparse{1/(#2*sqrt(2*pi))*exp(-((x-#1)^2)/(2*#2^2))}%
}
\begin{tikzpicture}
\begin{axis}[
no markers, domain=-5:5, samples=100,
axis lines*=left, xlabel=$x$, ylabel=$y$,
every axis y label/.style={at=(current axis.above origin),anchor=south},
every axis x label/.style={at=(current axis.right of origin),anchor=west},
height=5cm, width=12cm,
xtick={-1,+1}, ytick=\empty,
enlargelimits=false, clip=false, axis on top,
grid = major
]
\addplot [fill=black!20, draw=none, domain=-2.5:0] {gauss(+1,1)} \closedcycle ;
\addplot [fill=black!20, draw=none, domain=0:+2.5] {gauss(-1,1)} \closedcycle ;
\addplot [very thick,black!50!black] {gauss(-1,1)};
\addplot [very thick,black!50!black] {gauss(+1,1)};
\draw (-.2,2.5) node[left] {$f_Y(u)$};
\draw (3,-.5) node[below] {$u$};
\draw[->] (0,0) -- (6.2,0) node[right] {};
\draw[->] (0,0) -- (0,5) node[above] {};
\draw [yshift=-0.6cm, latex-latex](axis cs:0,0) -- node [fill=white] {$1.96\sigma$} (axis cs:0,0);
\end{axis}
\end{tikzpicture}
\end{document}
答案1
这可能有助于实现您的目标。我让您自己在 x 轴上进行标记,因为没有任何东西可以指示每个间隔的大小。
该图使用fill between
库来pgfplots
生成填充区域。我为关键参数定义了一些变量,这将使您更容易进行自定义。
结果如下:
这是代码:
\documentclass[border=5mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\usetikzlibrary{patterns}
\usepgfplotslibrary{fillbetween}
\begin{document}
\pgfmathdeclarefunction{gauss}{2}{%
\pgfmathparse{1/(#2*sqrt(2*pi))*exp(-((x-#1)^2)/(2*#2^2))}%
}
\begin{tikzpicture}
\def\startx{-5} % lower end of domain
\def\endx{5} % upper end of domain
\def\camean{-1.0} % mean of left side distribution
\def\casigma{1.0} % sigma for left side distribution
\def\cbmean{1.0} % mean for right side distribution
\def\cbsigma{1.0} % sigma for right side distribution
\def\verticala{0.7} % x vale for first delimiter
\def\verticalb{1.3} % x value for second delimited
\begin{axis}[
domain=\startx:\endx,
samples=101,
ymax=0.5,
enlargelimits=false,
axis x line=middle,
axis y line=middle,
xtick={-1,1},
xticklabels={-1,1},
ytick={\empty},
xlabel=$y$,
height=5cm,
width=10cm
]
% Draw distributions
\addplot [name path=a,thin, smooth] {gauss(\camean,\casigma)};
\addplot [name path=b,thin, smooth] {gauss(\cbmean,\cbsigma)};%
% Draw vertical lines:
\draw [gray, semithick] (\verticala,0) -- (\verticala,0.4);
\draw [gray, semithick] (\verticalb,0) -- (\verticalb,0.4);
% create path for x axis:
\path[name path=axis] (axis cs:\startx,0) -- (axis cs:\endx,0);
% generate fills:
\addplot[darkgray] fill between[of=a and axis,soft clip={domain=\verticala:\verticalb}]; %
\addplot[pattern=north east lines] fill between[of=b and a,
soft clip={(\verticala,0) rectangle (\verticalb,0.5)}
];
% place the labels for each distribution
\node (a) at (-1,0.45) {$p(y|1)$};
\node (b) at (1,0.45) {$p(y|0)$};
\end{axis}
\end{tikzpicture}
\end{document}