在 Tikz 中用高斯分布绘制准确度和精确度的概率密度图

在 Tikz 中用高斯分布绘制准确度和精确度的概率密度图

我正在尝试生成这样的图表蒂克兹

在此处输入图片描述

我只能创建这样的图像,但这并不理想。如果您能帮助我,我将不胜感激。

在此处输入图片描述

\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 样式。此外,您还可以使用leftrightabovebelow预定义锚点来对齐文本。

要旋转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}

在此处输入图片描述

相关内容