填充函数和坐标之间的空间 - pgfplots

填充函数和坐标之间的空间 - pgfplots

我正在 pgfplots 中创建一个由一个函数和一组坐标组成的图,如此 MWE 所示

\documentclass[tikz]{standalone}
\usepackage{tikz, pgfplots}

\begin{document}
\begin{tikzpicture}
    \pgfmathsetmacro\pointa{exp(-1)}
    \pgfmathsetmacro\pointb{exp(-1.25)}
    \pgfmathsetmacro\pointc{exp(-1.5)}
    \pgfmathsetmacro\pointd{exp(-1.75)}
    \pgfmathsetmacro\pointe{exp(-2)}
    \pgfmathsetmacro\pointf{exp(-2.25)}
    \pgfmathsetmacro\pointg{exp(-2.5)}
    \pgfmathsetmacro\pointh{exp(-2.75)}
    \pgfmathsetmacro\pointi{exp(-3)}
    \pgfmathsetmacro\pointj{exp(-3.25)}
    \pgfmathsetmacro\pointk{exp(-3.5)}
    \pgfmathsetmacro\pointl{exp(-3.75)}
    \pgfmathsetmacro\pointm{exp(-4)}
    \pgfmathsetmacro\pointn{exp(-4.25)}
    \pgfmathsetmacro\pointo{exp(-4.5)}
    \pgfmathsetmacro\pointp{exp(-4.75)}
    \pgfmathsetmacro\pointq{exp(-5)}
    \begin{axis}[xlabel={Time difference (timing units)},
                 ylabel={counts (arbirtary units)},
                     xmin=0, xmax=5.5,
                     ymin=0, ymax=1.1,
                     domain={0:5},
                 samples=100,
                 grid=major
                 %axis lines=left
                 ]
        \addplot[mark=none, thick, color=red, dashed] {exp(-x)};\addlegendentry{Theoretical Distribution}
        \addplot[color=black, fill=black, only marks, mark=*] coordinates{
        (0,0)
        (0.25,0.025)
        (0.5, 0.125)
        (0.75,0.25)
        (1,\pointa)
        (1.25,\pointb)
        (1.5,\pointc)
        (1.75,\pointd)
        (2,\pointe)
        (2.25,\pointf)
        (2.5,\pointg)
        (2.75,\pointh)
        (3,\pointi)
        (3.25,\pointj)
        (3.5,\pointk)
        (3.75,\pointl)
        (4,\pointm)
        (4.25,\pointn)
        (4.5,\pointo)
        (4.75,\pointp)
        (5,\pointq)};\addlegendentry{Dummy experimental points}
    \end{axis}
\end{tikzpicture}
\end{document}

在此处输入图片描述

我想要做的是用灰色阴影填充图片中黑点和红色虚线之间的空间。然后我想为这个阴影区域添加一个图例条目。

你知道怎样做吗?

答案1

fillbetween库就是用于此类用途的。请注意,两个图都添加了name path,而第二个图则删除了fill=black。由于填充不适用于only marks图,因此我删除only marks并添加了draw opacity=0

请注意,您无需定义所有这些\point宏,而是可以在坐标列表中使用 eg 。需要额外的一组括号来隐藏解析器(1.5,{exp(-1.5)})中的括号。exp(..)

代码输出

\documentclass[tikz]{standalone}
\usepackage{pgfplots} % loads tikz
\usepgfplotslibrary{fillbetween}

\begin{document}
\begin{tikzpicture}
    \pgfmathsetmacro\pointa{exp(-1)}
    \pgfmathsetmacro\pointb{exp(-1.25)}
    \pgfmathsetmacro\pointc{exp(-1.5)}
    \pgfmathsetmacro\pointd{exp(-1.75)}
    \pgfmathsetmacro\pointe{exp(-2)}
    \pgfmathsetmacro\pointf{exp(-2.25)}
    \pgfmathsetmacro\pointg{exp(-2.5)}
    \pgfmathsetmacro\pointh{exp(-2.75)}
    \pgfmathsetmacro\pointi{exp(-3)}
    \pgfmathsetmacro\pointj{exp(-3.25)}
    \pgfmathsetmacro\pointk{exp(-3.5)}
    \pgfmathsetmacro\pointl{exp(-3.75)}
    \pgfmathsetmacro\pointm{exp(-4)}
    \pgfmathsetmacro\pointn{exp(-4.25)}
    \pgfmathsetmacro\pointo{exp(-4.5)}
    \pgfmathsetmacro\pointp{exp(-4.75)}
    \pgfmathsetmacro\pointq{exp(-5)}
    \begin{axis}[xlabel={Time difference (timing units)},
                 ylabel={counts (arbirtary units)},
                     xmin=0, xmax=5.5,
                     ymin=0, ymax=1.1,
                     domain={0:5},
                 samples=100,
                 grid=major
                 %axis lines=left
                 ]
        \addplot[mark=none, thick, color=red, dashed,name path=A] {exp(-x)};
        \addlegendentry{Theoretical Distribution}
        \addplot[color=black, draw opacity=0, mark=*,name path=B] coordinates{
        (0,0)
        (0.25,0.025)
        (0.5, 0.125)
        (0.75,0.25)
        (1,\pointa)
        (1.25,\pointb)
        (1.5,\pointc)
        (1.75,\pointd)
        (2,\pointe)
        (2.25,\pointf)
        (2.5,\pointg)
        (2.75,\pointh)
        (3,\pointi)
        (3.25,\pointj)
        (3.5,\pointk)
        (3.75,\pointl)
        (4,\pointm)
        (4.25,\pointn)
        (4.5,\pointo)
        (4.75,\pointp)
        (5,\pointq)};
        \addlegendentry{Dummy experimental points}

        \addplot [fill=black!20] fill between[of=A and B];
        \addlegendentry{Shaded area}
    \end{axis}
\end{tikzpicture}
\end{document}

稍有变化:将第二个图一分为二,明确坐标直到 x=1,exp(-x)之后的图为:

\documentclass[tikz,border=5mm]{standalone}
\usepackage{pgfplots} % loads tikz
\usepgfplotslibrary{fillbetween}

\begin{document}
\begin{tikzpicture}
    \begin{axis}[xlabel={Time difference (timing units)},
                 ylabel={counts (arbirtary units)},
                     xmin=0, xmax=5.5,
                     ymin=0, ymax=1.1,
                     domain={0:5},
                 samples=100,
                 grid=major,
                 dotplot/.style={
                   black, draw opacity=0, mark=*}
                 ]
        \addplot[mark=none, thick, color=red, dashed,name path=A] {exp(-x)};
        \addlegendentry{Theoretical Distribution}
        \addplot[dotplot,name path=B] coordinates{
        (0,0)
        (0.25,0.025)
        (0.5, 0.125)
        (0.75,0.25)
        (1,{exp(-1)})};
        \addplot [forget plot,dotplot,samples at={1,1.25,...,5}] {exp(-x)};
        \addlegendentry{Dummy experimental points}

        \addplot [fill=black!20] fill between[of=A and B,soft clip={domain=0:1}];
        \addlegendentry{Shaded area}
    \end{axis}
\end{tikzpicture}
\end{document}

相关内容