1

I have the following function plot:

\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}

Now I want to gray the area delimited by those 3 graphs, based on an automated computation of the intersection points, possibly using the fillbetween library. The solutions to similar questions I found do not work here, because we have 3 graphs involved (and not only two). Also, I'm interested in a clean solution that generalizes to analogous situations involving more than 3 graphs.

2
  • 1
    Does this answer your question? Fill space between three \draw lines with arbitrary intersection points
    – Pier Paolo
    Commented Nov 27, 2022 at 18:50
  • @PierPaolo Thank you for the pointer; it might be helpful indeed. However, the setting is a bit different. The other question it about paths in a pure tikz picture; here I have the paths given by pgfplot functions. It still would be great to have an answer adapted for this pgfplots setting, for people like me who are not too fluent in tikz/pgfplots.
    – azimut
    Commented Nov 27, 2022 at 19:37

2 Answers 2

2
\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}

enter image description here

2
  • Many thanks for this answer! It works. Would you mind adding a short explanation? Why the scope is needed, and what does the \clip command (I've never seen it before).
    – azimut
    Commented Nov 27, 2022 at 20:12
  • 1
    @azimut Without scope the \clip command clips all subsequent drawing. For the \clip command see tikz manual “2.11 Clipping a Path” p. 37
    – polyn
    Commented Nov 27, 2022 at 20:47
1

Like this:

enter image description here

Code:

\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}
2
  • Many thanks for your answer. It looks good, but in the code I see the values .2, .267 and .577. I guess you have computed the x values of the intersection points manually. As written in my question, I'm really looking for a solution with "automated computation of the intersection points".
    – azimut
    Commented Nov 27, 2022 at 18:15
  • @azimut I searched for a while but I dont find nothing for this. I hope You find better things... Commented Nov 27, 2022 at 18:25

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .