如何在 pgfplot 中添加节点

如何在 pgfplot 中添加节点

我想将下面的图片转换为 LaTeX,它几乎完成了。但是我无法像图片中那样为 h_0,025 和 h_0,975 添加节点/标记。每当我添加 \node ... LaTeX 就会崩溃并且不会停止加载。有人知道怎么做吗?

在此处输入图片描述

\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=-4:4, 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={0.5}, ytick=\empty,
  enlargelimits=false, clip=false, axis on top,
  grid = major
  ]  
  \addplot [fill=cyan!20, draw=none, domain=-4:4] {gauss(0.5,1)} 
  \closedcycle;
  \addplot [fill=white, draw=none, domain=-1.1:2.1] {gauss(0.5,1)} 
  \closedcycle;

  \addplot [very thick,cyan!50!black] {gauss(0.5,1)};

  \draw [yshift=-0.6cm, latex-latex](axis cs:4,0) -- node [fill=white] 
  {\small{0,025 Qantil}} (axis cs:2.1,0);
  \end{axis}

 \end{tikzpicture}
\end{document}

答案1

不幸的是,您没有提供失败的代码(以注释形式),所以我们不确切知道您尝试做什么,因此无法猜测为什么您的代码会导致 LaTeX 崩溃。

有很多方法可以添加您想要添加的标签和线条。这里我介绍一种方法,使用法线xticks将标签与轴外的小“线”放在一起,并从 x 轴向上绘制额外的垂直线。为了将标签放置在“中间区域”,我使用了一个法线节点。有关更多详细信息,请查看代码中的注释。

% used PGFPlots v1.15
\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
    \usetikzlibrary{calc}
    \pgfplotsset{
        % use this `compat' level or higher so one don't has to prefix TikZ
        % coordinates by `axis cs:'
        compat=1.11,
        % (moved declared function here ...
        /pgf/declare function={
            % ... and added argument `\x'.
            gauss(\x,\mean,\std) = 1/(\std*sqrt(2*pi))*exp(-((\x-\mean)^2)/(2*\std^2));
            % Also created a dummy short version of your used function)
            MyGauss(\x) = gauss(\x,0.5,1);
        },
    }
\begin{document}
\begin{tikzpicture}
    \begin{axis}[
        height=5cm,
        width=12cm,
        no markers,
        domain=-4:4,
        samples=101,
        axis lines*=left,
        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,
        },
        % added the other two values here ...
        xtick={-1.1,0.5,2.1},
        % ... and stated the corresponding labels
        xticklabels={
            $h_{0,025}$,
            $\pgfmathprintnumber{0.5}$,
            $h_{0,975}$
        },
        ytick=\empty,
        % place the ticks only outside ...
        tick align=outside,
        % ... using this length
        major tick length=2mm,
        % this is to set the base line of the normal and extra ticks to
        % the same level/height
        typeset ticklabels with strut,
        % this is to use a comma as decimal separator
        /pgf/number format/use comma,
        enlargelimits=false,
        clip=false,
        axis on top,
    ]
        % filled areas at the sides
        \addplot [fill=cyan!20,draw=none,domain=-4:-1.1] {MyGauss(\x)}
            \closedcycle;
        \addplot [fill=cyan!20,draw=none,domain=2.1:4]   {MyGauss(\x)}
            \closedcycle;

        % line plot of function
        \addplot [very thick,cyan!50!black] {MyGauss(\x)};

        % ----------
        % draw the vertical lines at the ticks
        \draw [help lines]
            (-1.1,0) -- ($ (-1.1,0)!1.5!(-1.1,{MyGauss(-1.1)}) $)
            ( 2.1,0) -- ($ ( 2.1,0)!1.5!( 2.1,{MyGauss( 2.1)}) $)
            ( 0.5,0) -- (0.5,{MyGauss(0.5)})
        ;

        % node in "middle" area
        \node at (0.5,{MyGauss(0.5)/2}) {\pgfmathprintnumber{0.95}};

        % (shifted this a bit more down so the text isn't that near to each other)
        \draw [yshift=-9mm, latex-latex] (axis cs:4,0)
            -- node [fill=white]
                {\small{\pgfmathprintnumber[fixed,precision=3]{0.025} Quantil}}
                    (axis cs:2.1,0);
    \end{axis}
\end{tikzpicture}
\end{document}

该图显示了上述代码的结果

相关内容