5

How to shade the intersection portion?

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

\begin{document}
\begin{tikzpicture}
\begin{axis}[xmax=9,ymax=9, samples=50]
  \addplot[blue, ultra thick] (x,x*x);
  \addplot[red,  ultra thick] (x*x,x);
\end{axis}
\end{tikzpicture}
\end{document}

Output:

enter image description here

  • 1
    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
6

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:

enter image description here

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.