[Tex/LaTex] How to draw areas between curves of the form $x=f(y)$ and $x=g(y)$

fillbetweenpgfplots

I want to shade the region between two curves whose equations are of the form $x=f(y)$ and $x=g(y)$, where $f(y)\leq g(y)$ for all $y\in [c,d]$. I heard that pgfplots can be used. Also, the fillbetween library doesn't load in my Mac. Can you help me with this, guys? I want something like this one:

areas using horizontal strips

I used GeoGebra to generate the image. Thanks a lot!

Best Answer

Here is a suggestion using the fillbetween library and soft clip to domain y:

\addplot[thick,name path=P] ({.5*(x-2)^2+.5},{x})node[pos=0.92,above]{x=f(y)};
\addplot[thick,name path=Q]({-.3*(x-2)^2+2},{x})node[pos=0.94,above]{x=g(y)};
\addplot[red!80!black!40] fill between [of=P and Q, soft clip={domain y=\C:\D},reverse=true];

enter image description here

Code:

\documentclass[margin=5mm]{standalone}

\usepackage{lmodern} 
\usepackage{pgfplots}
\pgfplotsset{compat=1.10}% version 1.10 of pgfplots is needed
\usepgfplotslibrary{fillbetween}

\newcommand\C{}
\newcommand\D{}

\begin{document}
\begin{tikzpicture}
  \begin{axis}[
      xmin=-.5,xmax=3,
      axis lines*=middle,
      xtick=\empty,
      ytick=\empty,
      height=10cm,
      samples=200,
    ]
    \def\C{1}
    \def\D{3}
    \addplot[thick,name path=P] ({.5*(x-2)^2+.5},{x})node[pos=0.92,above]{x=f(y)};
    \addplot[thick,name path=Q]({-.3*(x-2)^2+2},{x})node[pos=0.94,above]{x=g(y)};
    \addplot[red!80!black!40] fill between [of=P and Q, soft clip={domain y=\C:\D},reverse=true];
    \draw({rel axis cs:0,0}|-{axis cs:0,\C})--({rel axis cs:1,1}|-{axis cs:0,\C});
    \draw({rel axis cs:0,0}|-{axis cs:0,\D})--({rel axis cs:1,1}|-{axis cs:0,\D});
    \node[above left]at(axis cs:0,\C){c};
    \node[above left]at(axis cs:0,\D){d};
    \node at (axis cs:1.25,2){R};
  \end{axis}
\end{tikzpicture}
\end{document}
Related Question