将基于 pgfplots 的曲线限制在指定的轴上

将基于 pgfplots 的曲线限制在指定的轴上

我是 pgfplots 的新手,在弄清楚如何将曲线限制在某个 x 轴范围内时遇到了一些麻烦。我创建了一个基于 pgfplots 的图,如下所示

初稿图片

它基于以下tex文件:

\documentclass{standalone}              
\usepackage{pgfplots}


% useful commands and definitions
\pgfplotsset{compat=1.10}
\pgfmathdeclarefunction{gauss}{2}{%
    \pgfmathparse{1/(#2 *sqrt(2*pi)) * exp(-((x-#1)^2)/(2*#2^2))}%
}

\begin{document}

\begin{tikzpicture}     
        \begin{axis}[
            height=5cm, width=12cm,
            no markers, domain=0:10, samples=100,
            axis lines*=left,
            ylabel=$y$,
            ymax=0.45,
            hide y axis,
            every axis x label/.style={
               at=(current axis.right of origin),
               anchor=west
            },
            xlabel=$x$,
            xtick={3,6.5},
            xticklabels={$\mu_0$, $\mu_1$},
            ytick=\empty,
            enlargelimits=false, clip=false, axis on top,
            grid = major
        ]
        \addplot[very thick, cyan!50!black]{gauss(6.5,1)};

        \node[above, red!50!black] at (axis cs:3,0.45) {Reference Value};
        \node[above, red!50!black] at (axis cs:6.5,0.45) {Sensor Value};

        \draw[thick,arrows={<->}] (axis cs:3,0.3) -- (axis cs:6.5,0.3)
              node[pos=0.5,above] {Accuracy};

        \draw[thick,arrows={<->}] (axis cs:4.9,0.1) -- (axis cs:8.1,0.1)
              node[pos=0.5,below] {Precision};

        \end{axis}
\end{tikzpicture}

\end{document}

该图片包含一个从 开始的 x 轴范围(0,10),并有一个蓝色的法线密度(高斯)曲线,其平均值位于刻度点\mu_1 = 6.5。我想将高斯曲线限制为\mu_0 = 3到 10,而不是从 开始渲染(0,10)

我尝试将高斯曲线的语句从:

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

到:

\addplot[very thick, cyan!50!black, domain=3:10]{gauss(6.5,1)};

来达到这个效果,但是现在的图是这样的:

代码调整

x 轴的范围是从 (3,10),而不是 (0,10)。如何使图片看起来像:

想要的图片

我在这里使用 gimp 手动删除了(0,3)或中的蓝线。(0,\mu_0)

答案1

domain=3:10在选项中使用\addplot并放入xmin=0,xmax=10轴选项。

\documentclass{standalone}
\usepackage{pgfplots}

% useful commands and definitions
\pgfplotsset{compat=1.10}
\pgfmathdeclarefunction{gauss}{2}{%
    \pgfmathparse{1/(#2 *sqrt(2*pi)) * exp(-((x-#1)^2)/(2*#2^2))}%
}

\begin{document}

\begin{tikzpicture}
        \begin{axis}[
            height=5cm, width=12cm,
            no markers,  samples=100,
            xmin=0,xmax=10,  %% <-------------------------
            axis lines*=left,
            ylabel=$y$,
            ymax=0.45,
            hide y axis,
            every axis x label/.style={
               at=(current axis.right of origin),
               anchor=west
            },
            xlabel=$x$,
            xtick={3,6.5},
            xticklabels={$\mu_0$, $\mu_1$},
            ytick=\empty,
            enlargelimits=false, clip=false, axis on top,
            grid = major
        ]
        \addplot[very thick, cyan!50!black,domain=3:10]{gauss(6.5,1)};

        \node[above, red!50!black] at (axis cs:3,0.45) {Reference Value};
        \node[above, red!50!black] at (axis cs:6.5,0.45) {Sensor Value};

        \draw[thick,arrows={<->}] (axis cs:3,0.3) -- (axis cs:6.5,0.3)
              node[pos=0.5,above] {Accuracy};

        \draw[thick,arrows={<->}] (axis cs:4.9,0.1) -- (axis cs:8.1,0.1)
              node[pos=0.5,below] {Precision};

        \end{axis}
\end{tikzpicture}

\end{document}

在此处输入图片描述

相关内容