# How to draw areas between curves of the form $x=f(y)$ and $x=g(y)$?

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:

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

• pgfplots has fillbetween facility! – user11232 Jul 17 '14 at 9:44
• Hi. But the fillbetween library doesn't seem to load. I always receive an error message. I also think that it can be used only for shading areas between curves of the form $y=f(x)$ and $y=g(x)$. Thanks for replying quick! – John Patrick Sta Maria Jul 17 '14 at 9:45
• Have you tried any code? If so, can you please add it to the question? It will be helpful for those who try to help you. :) – user11232 Jul 17 '14 at 9:47
• Take a look at tex.stackexchange.com/a/163654/1952 and read feellbetween library section of pgfplots documentation. – Ignasi Jul 17 '14 at 10:22
• Why don't you just use the nice graph you have created with GeoGebra? – Thruston Jul 17 '14 at 20:47

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

\addplot[red!80!black!40] fill between [of=P and Q, soft clip={domain y=\C:\D},reverse=true];

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[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}
• Thank you for your comments! They are overwhelming. In particular, I tried compiling this code in my Mac using TeXshop but I got a lot of error messages and there was no even a single output generated. – John Patrick Sta Maria Jul 20 '14 at 6:34
• Which version of pgfplots do you have? At least Version 1.10 is needed to run the code above. That's why I added \pgfplotsset{compat=1.10}. – esdd Jul 20 '14 at 10:37
• Would you know how to update the pgfplots package? I don't know where to see the current version number. I think the fillbetween library isn't loading. Thanks! – John Patrick Sta Maria Jul 20 '14 at 10:59
• You can find the number of your version in the log file. If you add \listfiles to the preamble of your document a compact list of all loaded files ist printed at the end of the log file. Use the package manager of your TeX distribution to update the package. Version 1.10 of pgfplots is also available on CTAN. – esdd Jul 20 '14 at 15:00
• @JohnPatrickStaMaria Or put \pgfplotsversion in the body of your document somewhere. – percusse Jul 20 '14 at 15:36

Here's something to get you started, which uses the fillbetween library mentioned in the comments:

% arara: pdflatex
% !arara: indent: {overwrite: yes, localSettings: yes}
\documentclass{standalone}

% graphs
\usepackage{pgfplots}
\usepgfplotslibrary{fillbetween}

% arrow style
\tikzset{>=stealth}

\begin{document}

% $x = y^2-4y$, $x = 2y-y^2$

\begin{tikzpicture}
\begin{axis}[
axis x line=middle,
axis y line=middle,
xlabel={$x$},
ylabel={$y$},
axis line style={<->},
xmin=-5,xmax=5,
ymin=-4,ymax=8,
xtick={-4,-2,...,2,4},
ytick={-2,2,4,6},
minor xtick={-3,-1,...,3},
minor ytick={-3,-1,...,7},
grid=both,
samples=100,
]
\addplot[name path=B,domain=0:3,ultra thick] ({2*x-x^2},{x});
\addplot[fill=blue!50] fill between[of=A and B];
\addplot[domain=-1:5,samples=50]({x^2-4*x},{x}) node[pos=.85,anchor=south] {$x=f(y)$};
\addplot[domain=-1.4:3.4,samples=50,ultra thick] ({2*x-x^2},{x}) node[pos=.1,anchor=north] {$x=g(y)$};
\end{axis}
\end{tikzpicture}

\end{document}

You'll see that I had to plot each curve twice - once for the shaded area, and once for the parts of the curves that go beyond the shaded area. You can certainly try plotting each curve just once, but the fill between part struggles (perhaps a guru has an improvement).

Here's another version that doesn't use the fillbetween library - very much the same idea, but perhaps not quite as nice with the result.

% arara: pdflatex
% !arara: indent: {overwrite: yes, localSettings: yes}
\documentclass{standalone}

% graphs
\usepackage{pgfplots}

% arrow style
\tikzset{>=stealth}

\begin{document}

% $x = y^2-4y$, $x = 2y-y^2$

\begin{tikzpicture}
\begin{axis}[
axis x line=middle,
axis y line=middle,
xlabel={$x$},
ylabel={$y$},
axis line style={<->},
xmin=-5,xmax=5,
ymin=-4,ymax=8,
xtick={-4,-2,...,2,4},
ytick={-2,2,4,6},
minor xtick={-3,-1,...,3},
minor ytick={-3,-1,...,7},
grid=both,
samples=100,
axis on top,
]