How to shade the intersection portion? using tikz?

How to shade the intersection portion?

\documentclass[border=2pt]{standalone}
\usepackage{pgfplots}

\begin{document}
\begin{tikzpicture}
\begin{axis}[xmax=9,ymax=9, samples=50]
\end{axis}
\end{tikzpicture}
\end{document}


Output:

• Take a look at the fillbetween pgfplots library. – Henri Menke Jul 27 '16 at 6:59
• @HenriMenke though this would be my choice as well, it does not really work for this example. There are the wrong segements colored and even setting reverse=true does not give the correct result. Maybe you want to provide an answer or try as well? – thewaywewalk Jul 27 '16 at 9:16

Here is a suggestion using intersection segments. It needs the pgfplots library fillbetween.

\documentclass[border=2pt]{standalone}
\usepackage{pgfplots}
\usepgfplotslibrary{fillbetween}

\begin{document}
\begin{tikzpicture}
\begin{axis}[xmax=9,ymax=9, samples=50,set layers]
\addplot[name path=plot 1,blue, ultra thick] (x,x*x);
\addplot[name path=plot 2, red,  ultra thick] (x*x,x);
\begin{pgfonlayer}{axis tick labels}
\fill[green,
intersection segments={
of=plot 1 and plot 2,
sequence={L2--R2[reverse]}
}];
\end{pgfonlayer}
\end{axis}
\end{tikzpicture}
\end{document}


Result:

• thank you so much... will it work if I have more than two curves?? any reference links?? – David Jul 28 '16 at 4:51