[Tex/LaTex] Shaded area in graph behind axes and plot, plot in front of axes

pgfplotstikz-pgf

I want part of the graph shaded. The problem is of course that shade is above axis and grid, like in MWE below. Of course, I also tried axis on top option, but the problem is that in this case plot is behind grids. Is there any way to order this correctly, i.e. first shade, then axes and in the end plot?

\documentclass{standalone}
\usepackage{pgfplots}
\begin{document}

\begin{tikzpicture}
\begin{loglogaxis}[domain=1:100,xmin=1,xmax=100,ymin=1,ymax=100,grid]
  \fill[color=black!2] (axis cs:5,1) rectangle (axis cs:20,100);
  \addplot[black] coordinates {(1,1) (5,10) (20,10) (100,100)};  
  \node at (axis cs:10,10) [above] {constant};
\end{loglogaxis}
\end{tikzpicture}

\end{document}

enter image description here

Best Answer

Put shaded area on background layer:

\documentclass{standalone}
\usepackage{pgfplots}
\pgfdeclarelayer{background}% determine background layer
\pgfsetlayers{background,main}% order of layers

    \begin{document}
\begin{tikzpicture}
\begin{loglogaxis}[domain=1:100,xmin=1,xmax=100,ymin=1,ymax=100,grid]
\begin{pgfonlayer}{background}
  \fill[color=black!10] (axis cs:5,1) rectangle (axis cs:20,100);
\end{pgfonlayer}
  \addplot[black] coordinates {(1,1) (5,10) (20,10) (100,100)};
  \node at (axis cs:10,10) [above] {constant};
\end{loglogaxis}
\end{tikzpicture}
    \end{document}

enter image description here

Related Question