![在 Tikz 中用高斯分布绘制准确度和精确度的概率密度图](https://linux22.com/image/453723/%E5%9C%A8%20Tikz%20%E4%B8%AD%E7%94%A8%E9%AB%98%E6%96%AF%E5%88%86%E5%B8%83%E7%BB%98%E5%88%B6%E5%87%86%E7%A1%AE%E5%BA%A6%E5%92%8C%E7%B2%BE%E7%A1%AE%E5%BA%A6%E7%9A%84%E6%A6%82%E7%8E%87%E5%AF%86%E5%BA%A6%E5%9B%BE.png)
我正在尝试生成这样的图表蒂克兹:
我只能创建这样的图像,但这并不理想。如果您能帮助我,我将不胜感激。
\documentclass[border=3pt,tikz]{standalone} %[dvipsnames]
\usepackage{amsmath} % for \dfrac
\usepackage{tikz}
\tikzset{>=latex} % for LaTeX arrow head
\usepackage{pgfplots} % for the axis environment
\usepackage{xcolor}
\usepackage[outline]{contour} % halo around text
\contourlength{1.2pt}
\usetikzlibrary{positioning,calc}
\usetikzlibrary{backgrounds}% required for 'inner frame sep'
\pgfmathdeclarefunction{gauss}{3}{%
\pgfmathparse{1/(#3*sqrt(2*pi))*exp(-((#1-#2)^2)/(2*#3^2))}%
}
% to fill an area under function
\usepgfplotslibrary{fillbetween}
\usetikzlibrary{patterns}
\pgfplotsset{compat=1.12} % TikZ coordinates <-> axes coordinates
\begin{document}
\begin{tikzpicture}
\message{Normal distributions, different mu^^J}
\def\q{5};
\def\B{3};
\def\S{7};
\def\Bs{1.0};
\def\Ss{1.0};
\def\xmax{\S+3.2*\Ss};
\def\ymin{{-0.15*gauss(\B,\B,\Bs)}};
\begin{axis}[every axis plot post/.append style={
mark=none,domain={-0.05*(\xmax)}:{1.08*\xmax},samples=50,smooth},
xmin={-0.1*(\xmax)}, xmax=\xmax,
ymin=\ymin, ymax={1.1*gauss(\B,\B,\Bs)},
axis lines=middle,
axis line style=thick,
enlargelimits=upper, % extend the axes a bit to the right and top
ticks=none,
ylabel=Probability density,
xlabel=value,
x label/.style={at={(current axis.right of origin)},anchor=north west},
width=0.7*\textwidth, height=0.5*\textwidth,
clip=false, % prevent labels falling off
y=200pt
]
% PLOTS
\addplot[red, name path=S,thick] {gauss(x,\S,\Ss)};
% LABELS
\node[above=2pt,black!20!red] at (1.05*\S,{gauss(\S,\S,\Ss)}) {$\mathcal{N}(\mu,\sigma)$};
\end{axis}
\end{tikzpicture}
\end{document}
答案1
您尝试的x label/.style
很接近,但有一个键值,x label style=
而不是明确的 tikz 样式。此外,您还可以使用left
、right
、above
、below
预定义锚点来对齐文本。
要旋转ylabel
,您只需添加rotate=90
其样式即可。
添加额外的线条也很简单。
\documentclass[border=3pt,tikz]{standalone} %[dvipsnames]
\usepackage{amsmath} % for \dfrac
\usepackage{tikz}
\tikzset{>=latex} % for LaTeX arrow head
\usepackage{pgfplots} % for the axis environment
\usepackage{xcolor}
\usepackage[outline]{contour} % halo around text
\contourlength{1.2pt}
\usetikzlibrary{positioning,calc}
\usetikzlibrary{backgrounds}% required for 'inner frame sep'
\pgfmathdeclarefunction{gauss}{3}{%
\pgfmathparse{1/(#3*sqrt(2*pi))*exp(-((#1-#2)^2)/(2*#3^2))}%
}
% to fill an area under function
\usepgfplotslibrary{fillbetween}
\usetikzlibrary{patterns}
\pgfplotsset{compat=1.12} % TikZ coordinates <-> axes coordinates
\begin{document}
\begin{tikzpicture}
\message{Normal distributions, different mu^^J}
\def\q{5};
\def\B{3};
\def\S{7};
\def\Bs{1.0};
\def\Ss{1.0};
\def\xmax{\S+3.2*\Ss};
\def\ymin{{-0.15*gauss(\B,\B,\Bs)}};
\begin{axis}[every axis plot post/.append style={
mark=none,domain={-0.05*(\xmax)}:{1.08*\xmax},samples=50,smooth},
xmin={-0.1*(\xmax)}, xmax=\xmax,
ymin=\ymin, ymax={1.1*gauss(\B,\B,\Bs)},
axis lines=middle,
axis line style=thick,
enlargelimits=upper, % extend the axes a bit to the right and top
ticks=none,
ylabel=Probability density,
xlabel=value,
x label style={below},
y label style={above left, rotate=90},
width=0.7*\textwidth, height=0.5*\textwidth,
clip=false, % prevent labels falling off
y=200pt
]
% PLOTS
\addplot[red, name path=S,thick] {gauss(x,\S,\Ss)};
% LABELS
\node[above=2pt,black!20!red] at (1.05*\S,{gauss(\S,\S,\Ss)}) {$\mathcal{N}(\mu,\sigma)$};
% EXTRA LINES
\draw[<->, thick] (\S-3*\Ss, -0.02) -- node[below] {Precision} (\S+3*\Ss, -0.02);
\draw[thick, dash dot] (\S, 0) -- (\S, 0.4);
\draw[thick, green] (\B, 0) -- (\B, 0.4);
\draw[<->, thick] (\B, 0.3) -- node[above] {Accuracy} (\S, 0.3);
\end{axis}
\end{tikzpicture}
\end{document}