pgfplots:“填充之间”与范围和 foreach 循环的相互作用

pgfplots:“填充之间”与范围和 foreach 循环的相互作用

我打算fill between使用foreach循环在不同的地方创建多个绘图,每个绘图都有区域。虽然所有其他绘图线在循环的每次迭代中都按预期出现,但区域并未填充。

将轴放置fill between在范围内时也会发生类似的事情。看看这个 MWE:

\documentclass[tikz]{standalone} 
\usepackage{pgfmath}
\usepackage{pgfplots}
\usepgfplotslibrary{fillbetween}
\pgfplotsset{compat=newest}

\begin{document} 
\begin{tikzpicture}

    \begin{axis}[at={(0cm,2cm)},
        axis x line=none, axis y line=none,
        width=5cm, height=2cm, clip=false, anchor=origin,
        xmin=0, xmax=1, ymin=-1, ymax=1,
        trig format plots=rad ]
    \addplot[blue, samples=21, domain=0:1, name path=sin1] plot {sin(pi*x)};
    \addplot[blue, samples=21, domain=0:1, name path=sin2] plot {-sin(pi*x)};
    \addplot[blue!10] fill between [of=sin1 and sin2];
    \end{axis}

\begin{scope}
    \begin{axis}[at={(0cm,1cm)},
        axis x line=none, axis y line=none,
        width=5cm, height=2cm, clip=false, anchor=origin,
        xmin=0, xmax=1, ymin=-1, ymax=1,
        trig format plots=rad ]
    \addplot[blue, samples=21, domain=0:1, name path=sin3] plot {sin(pi*x)};
    \addplot[blue, samples=21, domain=0:1, name path=sin4] plot {-sin(pi*x)};
    \addplot[blue!10] fill between [of=sin3 and sin4];
    \end{axis}
\end{scope}

\foreach \j in {1} {
    \begin{axis}[at={(0cm,0cm)},
        axis x line=none, axis y line=none,
        width=5cm, height=2cm, clip=false, anchor=origin,
        xmin=0, xmax=1, ymin=-1, ymax=1,
        trig format plots=rad ]
    \addplot[blue, samples=21, domain=0:1, name path=sin5] plot {sin(\j*pi*x)};
    \addplot[blue, samples=21, domain=0:1, name path=sin6] plot {-sin(\j*pi*x)};
    \addplot[blue!10] fill between [of=sin5 and sin6];
    \end{axis}
}

\end{tikzpicture}
\end{document}

当第一个轴为不是注释掉后,所有区域都按预期填充。然而,当整个第一个轴注释掉后,另外两个轴上的两个区域未被填充。

有人知道为什么会发生这种情况吗?以及/或者如何在没有第一个轴的情况下使底部两个轴环境正常工作?

答案1

您可以将\foreach循环放在环境内,并仅在循环后axis使用。fill between

如果需要将整个内容包含axis在循环中,可以尝试使用\pgfplotsforeachungrouped\pgfplotsinvokeforeach。但是,我无法解释为什么这些宏在这种情况下有效而\foreach无效。

\documentclass[tikz]{standalone} 
\usepackage{pgfplots}
\usepgfplotslibrary{fillbetween}
\pgfplotsset{compat=newest}

\begin{document} 
\begin{tikzpicture}

\begin{axis}[at={(0cm,0cm)},
    axis x line=none, axis y line=none,
    width=5cm, height=2cm, clip=false, anchor=origin,
    xmin=0, xmax=1, ymin=-1, ymax=1,
    trig format plots=rad ]
\foreach \j in {1} {
    \addplot[blue, samples=21, domain=0:1, name path=sin5] plot {sin(\j*pi*x)};
    \addplot[blue, samples=21, domain=0:1, name path=sin6] plot {-sin(\j*pi*x)};
}
\addplot[blue!10] fill between [of=sin5 and sin6];
\end{axis}

\pgfplotsforeachungrouped \j in {1} {
    \begin{axis}[at={(0cm,1cm)},
        axis x line=none, axis y line=none,
        width=5cm, height=2cm, clip=false, anchor=origin,
        xmin=0, xmax=1, ymin=-1, ymax=1,
        trig format plots=rad ]
    \addplot[blue, samples=21, domain=0:1, name path=sin5] plot {sin(\j*pi*x)};
    \addplot[blue, samples=21, domain=0:1, name path=sin6] plot {-sin(\j*pi*x)};
    \addplot[blue!10] fill between [of=sin5 and sin6];
    \end{axis}
}

\pgfplotsinvokeforeach{1}{
    \begin{axis}[at={(0cm,2cm)},
        axis x line=none, axis y line=none,
        width=5cm, height=2cm, clip=false, anchor=origin,
        xmin=0, xmax=1, ymin=-1, ymax=1,
        trig format plots=rad ]
    \addplot[blue, samples=21, domain=0:1, name path=sin5] plot {sin(#1*pi*x)};
    \addplot[blue, samples=21, domain=0:1, name path=sin6] plot {-sin(#1*pi*x)};
    \addplot[blue!10] fill between [of=sin5 and sin6];
    \end{axis}
}

\end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容