我想绘制正态分布的双面创建区域(见下图)。我对蛇形刻度的正确对齐存在疑问,而且区域看起来可疑。我是不是漏掉了什么?任何帮助都将不胜感激。谢谢
\documentclass[border=5mm]{standalone}
\usepackage{pgfplots}
\usetikzlibrary{arrows,backgrounds,snakes}
\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=-3.2:3.2
, samples=100
, ymin=0
, axis lines*=left
, xlabel= $\textrm{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
]
\draw [help lines] (axis cs:-3.5, -0.4) grid (axis cs:3.5, 0.5);
\addplot [cyan!50!black] {gauss(x, 0, 1)};
\addplot [fill=cyan!20, draw=none, domain=-3:-1.96] {gauss(x, 0, 1)} \closedcycle;
\addplot [fill=cyan!20, draw=none, domain=1.96:3.2] {gauss(x, 0, 1)} \closedcycle;
\draw[snake=ticks, segment length=1cm] (axis cs:-3.1, 0) -- (axis cs:3.1, 0);
\node[below] at (axis cs:-2, -0.02) {$\overline{\textrm{X}} - 2\textrm{S}$};
\node[below] at (axis cs:-1, -0.02) {$\overline{\textrm{X}} - \textrm{S}$};
\node[below] at (axis cs:0, -0.02) {$\overline{\textrm{X}}$};
\node[below] at (axis cs:1, -0.02) {$\overline{\textrm{X}} + \textrm{S}$};
\node[below] at (axis cs:2, -0.02) {$\overline{\textrm{X}} + 2\textrm{S}$};
\draw (axis cs:-3.2, -0.15) --(axis cs:3.2, -0.15) ;
\node[right] at (axis cs:3.2, -0.15) {$\textrm{Z}$};
\draw[snake=ticks, segment length=1cm] (axis cs:-3.1, -0.15) -- (axis cs:3.1, -0.15);
\node[below] at (axis cs:-2, -0.16) {$-2$};
\node[below] at (axis cs:-1, -0.16) {$-1$};
\node[below] at (axis cs:0, -0.16) {$0$};
\node[below] at (axis cs:1, -0.16) {$1$};
\node[below] at (axis cs:2, -0.16) {$2$};
\end{axis}
\end{tikzpicture}
\end{document}
答案1
这个问题可能还有其他解决方案。不过,这是我的强力解决方案。
\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=-3.2:3.2
, samples=100
, ymin=0
, axis lines*=left
, xlabel=
, 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
]
%\draw [help lines] (axis cs:-3.5, -0.4) grid (axis cs:3.5, 0.5);
\addplot [cyan!50!black] {gauss(x, 0, 1)};
\pgfmathsetmacro\valueA{gauss(1, 0, 1)}
\pgfmathsetmacro\valueB{gauss(2, 0, 1)}
\node[right] at (axis cs:3.4, 0) {$\textrm{X}$};
\draw [dashed] (axis cs:1, 0) -- (axis cs:1, \valueA)
(axis cs:-1, 0) -- (axis cs:-1, \valueA);
\draw [dashed] (axis cs:2, 0) -- (axis cs:2, \valueB)
(axis cs:-2, 0) -- (axis cs:-2, \valueB);
\draw [latex-latex](axis cs:-1, 0.15) -- node [fill=white] {\footnotesize $68.27\%$} (axis cs:1, 0.15);
\draw [latex-latex](axis cs:-2, 0.03) -- node [fill=white] {\footnotesize $95.45\%$} (axis cs:2, 0.03);
\node[below] at (axis cs:-3, -0.02) {$\overline{\textrm{X}} - 3\textrm{S}$};
\node[below] at (axis cs:-2, -0.02) {$\overline{\textrm{X}} - 2\textrm{S}$};
\node[below] at (axis cs:-1, -0.02) {$\overline{\textrm{X}} - \textrm{S}$};
\node[below] at (axis cs:0, -0.02) {$\overline{\textrm{X}} $};
\node[below] at (axis cs:1, -0.02) {$\overline{\textrm{X}} + \textrm{S}$};
\node[below] at (axis cs:2, -0.02) {$\overline{\textrm{X}} + 2\textrm{S}$};
\node[below] at (axis cs:3, -0.02) {$\overline{\textrm{X}} + 3\textrm{S}$};
\draw (axis cs:-3.2, -0.15) --(axis cs:3.2, -0.15) ;
\node[right] at (axis cs:3.4, -0.15) {$\textrm{Z}$};
\draw[thick] (axis cs:-3.0, -0.01) -- (axis cs:-3.0, 0.01);
\draw[thick] (axis cs:-2.0, -0.01) -- (axis cs:-2.0, 0.01);
\draw[thick] (axis cs:-1.0, -0.01) -- (axis cs:-1.0, 0.01);
\draw[thick] (axis cs:-0.0, -0.01) -- (axis cs:0.0, 0.01);
\draw[thick] (axis cs:1.0, -0.01) -- (axis cs:1.0, 0.01);
\draw[thick] (axis cs:2.0, -0.01) -- (axis cs:2.0, 0.01);
\draw[thick] (axis cs:3.0, -0.01) -- (axis cs:3.0, 0.01);
\node[below] at (axis cs:-2, -0.16) {$-2$};
\node[below] at (axis cs:-3, -0.16) {$-3$};
\node[below] at (axis cs:-1, -0.16) {$-1$};
\node[below] at (axis cs:0, -0.16) {$0$};
\node[below] at (axis cs:1, -0.16) {$1$};
\node[below] at (axis cs:2, -0.16) {$2$};
\node[below] at (axis cs:3, -0.16) {$3$};
\draw[thick] (axis cs:-3.0, -0.16) -- (axis cs:-3.0, -0.14);
\draw[thick] (axis cs:-2.0, -0.16) -- (axis cs:-2.0, -0.14);
\draw[thick] (axis cs:-1.0, -0.16) -- (axis cs:-1.0, -0.14);
\draw[thick] (axis cs:-0.0, -0.16) -- (axis cs:0.0, -0.14);
\draw[thick] (axis cs:1.0, -0.16) -- (axis cs:1.0, -0.14);
\draw[thick] (axis cs:2.0, -0.16) -- (axis cs:2.0, -0.14);
\draw[thick] (axis cs:3.0, -0.16) -- (axis cs:3.0, -0.14);
\end{axis}
\end{tikzpicture}
\end{document}