[Tex/LaTex] pgfplots fillbetween with multiple curves

fillbetweenintersectionspgfplots

I have a TikZ picture like this:

\begin{tikzpicture}
  \begin{axis}[
    axis lines=middle,
    xmin=-0.5,xmax=3.5,ymin=-0.5,ymax=3.5
  ]
  \addplot[very thick,blue, samples=300, domain=0:3, name path=A] {sqrt(x)}; 
  \addplot[very thick,yellow!70!red, samples=300, domain=0:3, name path=B] {9-3*x}; 
  \addplot[very thick,red!90!teal, samples=300, domain=0:3, name path=C] {3}; 
  \addplot[very thick,green!70!magenta, samples=300, domain=0:3, name path=D] coordinates{(1/9,-6) (1/9,6)};
  \end{axis}
\end{tikzpicture}

the above tikzpicture, rendered

I’d like to fill the area enclosed by these four curves.

Similar questions have been answered using pgfplots’s fillbetween package, but it only seems to support filling between two curves. What are my options?

Best Answer

Here a workaround, you can divide your area on two parts: first between curve A and C, and the second between A and B and use soft clip to limit filling

\documentclass[border=5mm]{standalone}
\usepackage{pgfplots}
\usepgfplotslibrary{fillbetween}
\begin{document}

\begin{tikzpicture}
  \begin{axis}[
    axis lines=middle,
    xmin=-0.5,xmax=3.5,ymin=-0.5,ymax=3.5
  ]
  \addplot[very thick,blue, samples=300, domain=0:3, name path=A] {sqrt(x)}; 
  \addplot[very thick,yellow!70!red, samples=300, domain=0:3, name path=B] {9-3*x}; 
  \addplot[very thick,red!90!teal, samples=300, domain=0:3, name path=C] {3}; 
  \addplot[very thick,green!70!magenta, samples=300, domain=0:3, name path=D] coordinates{(1/9,-6) (1/9,6)};
  \addplot[gray!30] fill between[of=A and C,soft clip={domain=1/9:2}];
  \addplot[gray!30] fill between[of=A and B,soft clip={domain=2:2.5}];
  \end{axis}
\end{tikzpicture}

\end{document}    

enter image description here

Related Question