pgfplots:填充由 3 个图形分隔的区域

pgfplots:填充由 3 个图形分隔的区域

我有以下函数图:

\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.13}
\begin{document}
\begin{tikzpicture}
\begin{axis}[domain=0:0.8, ymin=0, ymax=2]
\addplot[color=blue, smooth, thick, samples=50] {1-2*x^2};
\addplot[color=red, smooth, thick, samples=50] {2-5*x^2};
\addplot[color=black, smooth, thick, samples=50] {3-30*x^2};
\end{axis}
\end{tikzpicture}
\end{document}

现在我想根据自动计算的交点(可能使用库)将这 3 个图所界定的区域变灰fillbetween。我找到的类似问题的解决方案在这里不起作用,因为我们涉及 3 个图(而不仅仅是两个)。此外,我对一个干净的解决方案感兴趣,该解决方案可以推广到涉及 3 个以上图的类似情况。

答案1

\documentclass[border=5mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\usepgfplotslibrary{fillbetween}
\begin{document}
    \begin{tikzpicture}
        \begin{axis}[domain=0:0.8, ymin=0, ymax=2]
            \addplot[draw=none,smooth,samples=50,name path=A] {1-2*x^2};
            \addplot[draw=none,smooth,samples=50,name path=B] {2-5*x^2};
            \addplot[draw=none,smooth,samples=50,name path=C] {3-30*x^2};
            \begin{scope}
                \clip[intersection segments={of=A and B, sequence={L1 -- R1[reverse]}}];
                \fill[gray!20,intersection segments={of=B and C, sequence={L2[reverse] -- R2}}];
            \end{scope}
            \addplot[color=blue, smooth, thick, samples=50] {1-2*x^2};
            \addplot[color=red, smooth, thick, samples=50] {2-5*x^2};
            \addplot[color=black, smooth, thick, samples=50] {3-30*x^2};
        \end{axis}
    \end{tikzpicture}
\end{document}

在此处输入图片描述

答案2

像这样:

在此处输入图片描述

代码:

\documentclass{book}

\usepackage{pgfplots}

\usepgfplotslibrary{fillbetween} % <-- this does the trick
\begin{document}
    \begin{tikzpicture}
        \begin{axis}[domain=0:0.8, ymin=0, ymax=2]
            \addplot[color=blue, smooth, thick, samples=50,name path=A] {1-2*x^2};
            \addplot[color=red, smooth, thick, samples=50,name path=B] {2-5*x^2};
            \addplot[color=black, smooth, thick, samples=50,name path=C] {3-30*x^2};
            \addplot[cyan!50] fill between [of=C and B,soft clip={domain=.2:.267}];
            \addplot[cyan!50] fill between [of=A and B,soft clip={domain=.267:.577}];
        \end{axis}
    \end{tikzpicture}
\end{document}

相关内容