# Area between curves tikz [duplicate]

I am trying to plot a graph using tikz, but I can't seem to be able to highlight the areas between the curves. The result should look something like this:

This is the code I have so far:

\begin{center}
\begin{tikzpicture}[scale=0.8]

\draw[->] (0,-0.5) -- (0,4.5) node[anchor=east] {};
\draw[->] (-0.5,0) -- (9,0) node[anchor=north] {};
\draw   (1,-0.2) node[anchor=north] {$a$}
(8,-0.2) node[anchor=north] {$b$}
;
\filldraw[fill=black!10](1,0)--(1,2)--(8,2)--(8,0);
\draw [](1,3) parabola bend (2.5,1.5)(4.2,2.4);
\draw [](4.2,2.4) parabola bend (5.5,3)(8,0);
\draw[] (1,0)--(1,3);
\end{tikzpicture}
\end{center}


The end result I would like to achieve is to have C and D higlighted in orange, while A and B in a dark grey. Don't worry about the axes labels or else, I mostly need help with the shading.

-

## marked as duplicate by Werner, Jubobs, Sean Allred, zeroth, Peter JanssonMar 6 at 19:22

Does tex.stackexchange.com/q/68462/1952 help? –  Ignasi Feb 27 at 22:35

For this, I'd suggest you to use the pgfplots package and stacking plots:

\documentclass[border=5mm]{standalone}
\usepackage{pgfplots}

\begin{document}

\pgfmathdeclarefunction{poly}{0}{%
\pgfmathparse{-x^3+5*(x^2)-3*x-3}%
}

\begin{tikzpicture}
\begin{axis}[
domain=-1.2:4.2,
ymin=-5,
ymax=10,
samples=160,
stack plots=y
]
% draw graph for the first function f
% draw graph of max(g-f, 0) and stack
% draw graph of min(g-f, 0) and stack
\end{axis}
\end{tikzpicture}

\end{document}


And with the help lines and labels:

\documentclass[border=5mm]{standalone}
\usepackage{pgfplots}

\begin{document}

\pgfmathdeclarefunction{poly}{0}{%
\pgfmathparse{-x^3+5*(x^2)-3*x-3}%
}

\begin{tikzpicture}
\begin{axis}[
xmin=-2,
xmax=5,
ymin=-5,
ymax=10,
axis y line=left,
axis x line=bottom,
xtick={-1.2,2,4.2},
xticklabels={$a$,$\zeta$,$b$},
ytick={3},
yticklabels={$f(\zeta)$},
samples=160
]

% draw graph for the first function f
% draw graph of max(g-f, 0) and stack
% draw graph of min(g-f, 0) and stack

\draw[help lines] (axis cs:-1.2,-5) -- (axis cs:-1.2,3);
\draw[help lines] (axis cs:2,-5) -- (axis cs:2,3);
\draw[help lines] (axis cs:4.2,-5) -- (axis cs:4.2,3);
\end{axis}
\end{tikzpicture}

\end{document}


-
Thanks, this is perfect! –  ruben1691 Mar 2 at 15:23

Gonzalo's idea with stacking plots is clever. And now there's a new way, with pgfplots 1.10 and its fillbetween library:

• draw the polygon function and give it a name
• draw the line (or any other function) and name it too
• add a fill between plot

The essence:

\usepgfplotslibrary{fillbetween}
...
of = poly and line,
split, % calculate segments
every even segment/.style = {orange!70},
every odd segment/.style  ={gray!60}
];


Complete example:

\documentclass[border=5mm]{standalone}
\usepackage{pgfplots}

\usepgfplotslibrary{fillbetween}
\begin{document}

\pgfmathdeclarefunction{poly}{0}{%
\pgfmathparse{-x^3+5*(x^2)-3*x-3}%
}

\begin{tikzpicture}
\begin{axis}[
xmin=-2,
xmax=5,
ymin=-5,
ymax=10,
axis y line=left,
axis x line=bottom,
xtick={-1.2,2,4.2},
xticklabels={$a$,$\zeta$,$b$},
ytick={3},
yticklabels={$f(\zeta)$},
samples=160
]