我正在尝试在TikZ
和中重现下图pgfplots
。
我的 MWE 和再生图如下:
\documentclass{standalone}
\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=0:6,
samples=100,
axis lines*=left,
xlabel=$x$,
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=\empty,
ytick=\empty,
enlargelimits=false,
clip=false,
axis on top,
grid = major
]
\addplot [very thick,cyan!50!black] {gauss(3, 1)};
\draw [yshift=1.4cm, latex-latex](axis cs:2, 0) -- node [fill=white] {$0.683$} (axis cs:4, 0);
\draw [yshift=0.3cm, latex-latex](axis cs:1, 0) -- node [fill=white] {$0.954$} (axis cs:5, 0);
\end{axis}
\node[below] at (1, 0) {$\mu - 2\sigma$};
\node[below] at (2, 0) {$\mu - \sigma$};
\node[below] at (3, 0) {$\mu$};
\end{tikzpicture}
\end{document}
问题
- 节点放置不正确。
- 如何删除 y 轴?
- 如何绘制垂直虚线?
任何帮助都将不胜感激。谢谢
答案1
- 正如 Qrrbrbirlbel 所说,将节点移入环境
axis
并使用(axis cs:...)
。或者,您可以将节点移入环境axis
并disabledatascaling
在axis
选项中设置。这样,您就可以保留坐标(但是,如果您的图中的值非常大,则此方法不起作用)。 - 用于
hide y axis
隐藏 y 轴 - 对于线条,我将重新定义
gauss
函数以采用第三个参数(值x
)。然后,您可以使用它\pgfmathsetmacro
来计算所需坐标处的分布值x
,并使用它来绘制线条:
\documentclass[border=5mm]{standalone}
\usepackage{pgfplots}
\begin{document}
\pgfmathdeclarefunction{gauss}{3}{%
\pgfmathparse{1/(#3*sqrt(2*pi))*exp(-((#1-#2)^2)/(2*#3^2))}%
}
\begin{tikzpicture}
\begin{axis}[
no markers,
domain=0:6,
samples=100,
ymin=0,
axis lines*=left,
xlabel=$x$,
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=\empty,
ytick=\empty,
enlargelimits=false,
clip=false,
axis on top,
grid = major,
hide y axis
]
\addplot [very thick,cyan!50!black] {gauss(x, 3, 1)};
\pgfmathsetmacro\valueA{gauss(1,3,1)}
\pgfmathsetmacro\valueB{gauss(2,3,1)}
\draw [gray] (axis cs:1,0) -- (axis cs:1,\valueA)
(axis cs:5,0) -- (axis cs:5,\valueA);
\draw [gray] (axis cs:2,0) -- (axis cs:2,\valueB)
(axis cs:4,0) -- (axis cs:4,\valueB);
\draw [yshift=1.4cm, latex-latex](axis cs:2, 0) -- node [fill=white] {$0.683$} (axis cs:4, 0);
\draw [yshift=0.3cm, latex-latex](axis cs:1, 0) -- node [fill=white] {$0.954$} (axis cs:5, 0);
\node[below] at (axis cs:1, 0) {$\mu - 2\sigma$};
\node[below] at (axis cs:2, 0) {$\mu - \sigma$};
\node[below] at (axis cs:3, 0) {$\mu$};
\end{axis}
\end{tikzpicture}
\end{document}