# pgfplots: stacking 'fill between' over filled curve

I have a set of filled-plots (MWE: red) and line-plots (MWE: blue) in a graph. Stacking order works fine according to the order of code lines. Additionally, I need to fill the area between two line-plots in front of the filled plot.

The fill between works fine, but only visible behind the filled-plot although drawn after the filled-plot.

(a) Is this a bug or a feature?

(b) How can I force the fill between to be visible on top?

MWE:

\documentclass[tikz]{standalone}
\usepackage{pgfplots}
\usepgfplotslibrary{fillbetween}
\begin{document}
\begin{tikzpicture}
\begin{axis}
% \addplot [draw = none, fill=red, opacity=0.5]
table[row sep=crcr]{%
1   1\\
1   2\\
2   2\\
2   1\\
}
--cycle;
\addplot [blue, name path = A]
table[row sep=crcr]{%
1.4 1.2\\
1.2 1.4\\
};
\addplot [blue, name path = B]
table[row sep=crcr]{%
1.8 1.6\\
1.6 1.8\\
};
fill between[ of = A and B ];
\end{axis}
\end{tikzpicture}
\end{document}


Result according to MWE:

Result with opacity to show correct fill between behind the filled plot:

• on behalf of all color blinds, you, sir, are mean :) – percusse Feb 26 '18 at 13:08
• sorry for that one @percusse, users are free to choose colors of their liking... for their solution posts, of course ;o) – hanshansen Feb 26 '18 at 13:13

To question (a):
This is a feature and in the following solution you will find out why.

To question (b):
If you just want everything to appear as per given order you could simply add fill between/on layer={main} to the axis options, as in my presented solution below.

The downside of doing this is, that then your "fill" will partly cover the "draw"s. Please zoom on one of the lines as proof. But that normally is an undesired behavior and the reason why the fill between stuff is by default drawn on the "lower level" pre main.

Thus marmot suggests the right approach to show everything "as desired", i.e. moving either the "red" stuff further back (on layer=pre main technically would have been enough) or move the "blue" stuff more to the front, e.g. add on layer=axis descriptions or higher with /pgfplots/layers/standard layers.

% used PGFPlots v1.15
\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
\usepgfplotslibrary{fillbetween}
\pgfplotsset{compat=1.15}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
fill between/on layer={main},
]
% \addplot [draw = none, fill=red, opacity=0.5]
table {
1   1
1   2
2   2
2   1
}
-- cycle;
\addplot [blue,ultra thick,name path = A]
table {
1.4 1.2
1.2 1.4
};
\addplot [blue,ultra thick, name path = B]
table {
1.8 1.6
1.6 1.8
};
\addplot [fill=green!25] fill between[ of = A and B ];
\end{axis}
\end{tikzpicture}
\end{document}


• thx for the explanation to (b), understood. In addition, your approach fill between/on layer={main} enables a workaround with invisible lines for fill between and redrawing the lines afterwards. Maybe bad coding style but avoids considering layers, which might be beneficial in more complex graphs/pictures. – hanshansen Feb 27 '18 at 8:20
• Am I right that this is just a comment of yours and that there is no question? Because I am not sure if I have understood everything right. – Stefan Pinnow Feb 27 '18 at 8:54
• sorry for the confusion, no more questions. It was just a comment how your solution/information could potentially be (ab)used to avoid multiple layers and still draw correct fill betweens. – hanshansen Feb 27 '18 at 9:43

You might want to put the plots on different layers.

\documentclass[tikz]{standalone}
\usepackage{pgfplots}
\usepgfplotslibrary{fillbetween}
\begin{document}
\begin{tikzpicture}
\begin{axis}
\addplot [draw = none, fill=red,on layer=axis background]
% \addplot [draw = none, fill=red, opacity=0.5]
table[row sep=crcr]{%
1   1\\
1   2\\
2   2\\
2   1\\
}
--cycle;
\addplot [blue, name path = A]
table[row sep=crcr]{%
1.4 1.2\\
1.2 1.4\\
};
\addplot [blue, name path = B]
table[row sep=crcr]{%
1.8 1.6\\
1.6 1.8\\
};