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.

azimut
  • 863
  • 10
  • 18

2 Answers2

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

polyn
  • 5,614
  • 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 Nov 27 '22 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 Nov 27 '22 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-2x^2}; \addplot[color=red, smooth, thick, samples=50,name path=B] {2-5x^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}

  • 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 Nov 27 '22 at 18:15
  • @azimut I searched for a while but I dont find nothing for this. I hope You find better things... – Raffaele Santoro Nov 27 '22 at 18:25