为 pgfplot 中绘制的线下方的空间着色

为 pgfplot 中绘制的线下方的空间着色

我在 pgfplot 上绘制了一些值,我想用红色标出线下的部分。如果能给出一些关于包或我需要查看的内容的建议就更好了。在这种情况下,谷歌搜索并没有给我带来太大帮助。

我的图表: 在此处输入图片描述

我的代码:

\documentclass{article}

\begin{document}
\usepackage{pgfplots}
\usetikzlibrary{datavisualization}
\usepackage{tikz}
\begin{tikzpicture}
\datavisualization [school book axes, visualize as smooth line]
    data {
       x, y
    -1.9,0.001
-1.8,0.001
-1.7,0.002
-1.6,0.001
-1.5,0.01
-1.4,0.004
-1.3,0.005
-1.2,0.008
-1.1,0.01
-1,0.014
-0.9,0.029
-0.8,0.037
-0.7,0.056
-0.6,0.119
-0.5,0.187
-0.4,0.391
-0.3,0.667
-0.2,1.062
-0.1,1.648
0,2.418
0.1,2.454
0.2,1.829
0.3,1.243
0.4,0.809
0.5,0.537
0.6,0.319
0.7,0.178
0.8,0.101
0.9,0.06
1,0.035
1.1,0.019
1.2,0.018
1.3,0.014
1.4,0.019
1.5,0.004
1.6,0.004
1.7,0.002
1.8,0.003
1.9,0.001
2,0.003
2.1,0.001
2.2,0.002
2.3,0.001
2.4,0.001
};
\end{tikzpicture}


\end{document}

我希望它看起来像这样:

在此处输入图片描述

答案1

例如,可以使用 PGFPlotsfillbetween库来完成此操作。还请查看 PGFPlots 手册中的相应部分或 TeX.SX 上带有标签的其他问题

% used PGFPlots v1.16
\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
    \usepgfplotslibrary{fillbetween}
    \pgfplotsset{
        /pgf/declare function={
            % normal distribution where \mean = mean and \stddev = sd}
            gauss(\mean,\stddev) = 1/sqrt(2*pi*\stddev^2) * exp(-((\x-\mean)^2)/(2*\stddev^2));
            % define xmin and xmax only once here and reuse them later
            xmin=-2;
            xmax=2;
        },
    }
\begin{document}
\begin{tikzpicture}
    \begin{axis}[
        axis x line=bottom,
        axis y line=middle,
        axis on top,
        xmin=xmin,
        xmax=xmax,
        domain=xmin:xmax,
        samples=100,
    ]
        \path[name path=axis] (axis cs:xmin,0) -- (axis cs:xmax,0);

        \addplot [
            thick,
            name path=gauss,
        ] {gauss(0.1, 0.25)};
        % instead of using a function here you could also give table values or read a (table) file
       % \addplot table {<table> or <file name>};

        \addplot [
            fill=black!10,
        ] fill between [
            of=axis and gauss,
        ];
    \end{axis}
\end{tikzpicture}
\end{document}

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

相关内容