# Subtract area from plot

The color of the 2 areas are added. Instead, I would like each new area to cover the previous ones with the color defined in the description.

\begin{tikzpicture}[domain=0.001:160, samples=100]
\begin{axis}[
legend style={draw=none, legend columns=2, at={(.9,.9)}}]
\addplot [fill=blue, opacity=.3, draw=none, forget plot] {.508-1.52398*exp(-(13702.2/x)^0.13266)} \closedcycle;
\addplot [fill=violet, opacity=.3, draw=none, forget plot] {.445-0.34900*exp(-(30.0706/x)^0.23650)} \closedcycle;
\end{axis}
\end{tikzpicture}

• I'm not sure I understand what you mean. If you want the second area to cover the first one without transparency you can delete the option opacity=0.3 or set it to 1 (opacity=1). – LucaD Oct 31 '15 at 17:25
• I mean that violet area is not violet, but violet+blue. I would like that each area color doesn't depend on the previous area. I can't delete opacity because I have to plot something else on these areas. – Stefano Oct 31 '15 at 17:54
• Whatever is plotted last goes on top (with opacity=1). You may need to use \pgfplotsextra for tikz commands. – John Kormylo Oct 31 '15 at 20:14

One way to do that (if I understand correctly your question) is to use the stack plots option. First you plot the violet area and then the blue one using the difference of the two functions where it is positive. To avoid repeating the functions the option declare function can be employed.

\documentclass{standalone}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}[domain=0.001:160, samples=100]
\begin{axis}[
legend style={draw=none, legend columns=2, at={(.9,.9)}},
declare function={
f1(\x) = .508-1.52398*exp(-(13702.2/\x)^0.13266);
f2(\x) = .445-0.34900*exp(-(30.0706/\x)^0.23650);
pos(\x) = ifthenelse(\x>0,\x,0);
},
stack plots=y,
]
\addplot [fill=violet, opacity=.3, draw=none, forget plot] {f2(x)} \closedcycle;
\addplot [fill=blue, opacity=.3, draw=none, forget plot] {pos(f1(x)-f2(x))} \closedcycle;
\end{axis}
\end{tikzpicture}
\end{document}


The resulting plot:

• I have this error ! Package PGF Math Error: Unknown function f2' (in 'f2(x)').` – Stefano Oct 31 '15 at 18:46
• @Stefano I don't know what to say. The code I posted works for me; which version of pgf and pgfplots are you using? – LucaD Oct 31 '15 at 18:52