pgfplots: Fill between breaks background layer

I'm using the fillbetween library of pgfplots and I want to show background rectangle of the enclosing tikzpicture. However, the \addplot fill between command seems to break the background layer. Using the show background rectangle option from the backgrounds library results in compilation error Package pgf Error: Sorry, the requested layer 'background' is not part of this list.

MWE:

\documentclass{article}
\usepackage{tikz,pgfplots}
\pgfplotsset{compat=1.16}
\usetikzlibrary{backgrounds}
\usepgfplotslibrary{fillbetween}

\begin{document}
\begin{tikzpicture}[show background rectangle]
\begin{axis}
\addplot fill between [of=A and B];
\end{axis}
\end{tikzpicture}
\end{document}


Using the low-level command \tikzfillbetween seems to prevent fillbetween from messing with the layers, but then I can't create legends for the fill.

A related question

Welcome to TeX.SE! You seem to be mixing two layer sets. One way to fix it is to add "background" (as opposed to "axis background") and also "pre main" (for whatever reason) to the example list from the pgfplots manual.

\documentclass{article}
\usepackage{tikz,pgfplots}
\pgfplotsset{compat=1.16}
\usetikzlibrary{backgrounds}
\usepgfplotslibrary{fillbetween}
% I just copied this from p. 410 of the pgfplots manual and
% added the layers "background" and "pre main"
\pgfplotsset{layers/standard/.define layer set={background,
axis background,axis grid,axis ticks,axis lines,axis tick labels,pre
main,main,
axis descriptions,axis foreground
}{
grid style={/pgfplots/on layer=axis grid},
tick style={/pgfplots/on layer=axis ticks},
axis line style={/pgfplots/on layer=axis lines},
label style={/pgfplots/on layer=axis descriptions},
legend style={/pgfplots/on layer=axis descriptions},
title style={/pgfplots/on layer=axis descriptions},
colorbar style={/pgfplots/on layer=axis descriptions},
ticklabel style={/pgfplots/on layer=axis tick labels},
axis background@ style={/pgfplots/on layer=axis background},
3d box foreground style={/pgfplots/on layer=axis foreground},
}}

\begin{document}
\begin{tikzpicture}[show background rectangle]
\begin{axis}
\addplot fill between [of=A and B];
\end{axis}
\end{tikzpicture}
\end{document}


The residual problem has, according to what I find, nothing to do backgrounds and layers. To see this, consider the MWE

\documentclass{article}
\usepackage{tikz,pgfplots}
\pgfplotsset{compat=1.16}
\usepgfplotslibrary{fillbetween}
\begin{document}
\begin{tikzpicture}
\begin{axis}[xshift=1cm,yshift=1cm]
%\addplot fill between [of=A and B]; % without this line, problem disappears
\end{axis}
\draw[thick,blue] (current axis.south west) rectangle (current axis.north east);
\end{tikzpicture}
\end{document}


That's fine. However, if you activate

\addplot fill between [of=A and B];


you'll get

This is, of course, odd, but in my opinion deserves a separate question.

• Thanks! Please see update for the problem with xshift/yshift. – James Qi Aug 4 '18 at 2:45
• @JamesQi I think you have found something really important. However, it does in my opinion not really go under well under the title "Fill between breaks background layer". As I try to explain in the addendum to my answer, this problem does not seem to be directly related to the background layer stuff. I'd like to encourage you to ask a separate on question for this, which may get more attention. – user121799 Aug 4 '18 at 3:34
• I've created a new question. – James Qi Aug 4 '18 at 23:28

As marmot already pointed out in his answer you mix up two things here. While the backgrounds library defines the additional layer background, fill between calls set layers which installs a set of layers. Unfortunately none of them is called background. But besides adding the background layer (manually) to the list of layers (as in marmot's answer), you could also redefine the layer on which the show background rectangle draws its stuff.

For details please have a look at the comments in the code.

% used PGFPlots v1.16
\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
\usetikzlibrary{
backgrounds,
pgfplots.fillbetween,
}
%    % uncomment, if you don't use fill between in a plot but the following
%    % redefinition should work anyway.
%    % It will activate layers for *all* plots.
%    \pgfplotsset{
%        set layers,
%    }
\makeatletter
% copied from tikzlibrarybackgrounds.code.tex' lines 105ff
\def\tikz@background@framed{%
\tikz@background@save%
% changed this layer from background' to axis background'
\pgfonlayer{axis background}
\path[style=background rectangle] (\tikz@bg@minx,\tikz@bg@miny) rectangle (\tikz@bg@maxx,\tikz@bg@maxy);
\endpgfonlayer
}
\makeatother
\begin{document}
\begin{tikzpicture}[
show background rectangle,
]
\begin{axis}[
%            % alternatively use set layers locally here instead of globally
%            % as stated in the preamble with \pgfplotsset'
%            set layers,
]