# Put grid below plot in pgfplots

In a plot with pgfplots I would like to draw, from background to foreground, in the following order:

1. (background) grid;
2. (intermediate layer) plot;
3. (foreground) axis.

Adding axis on top to the axis options means axis lines, ticks and grids are printed on top of the stuff inside the environment.

So, this is not the correct option for this. This code

\documentclass{article}
\usepackage{pgfplots}
\usetikzlibrary{shapes.geometric,arrows,arrows.meta}
\pgfplotsset{
/pgfplots/layers/Bowpark/.define layer set={
axis background,axis grid,main,axis ticks,axis lines,axis tick labels,
axis descriptions,axis foreground
}{/pgfplots/layers/standard},
}

\begin{document}

\begin{tikzpicture}
\begin{axis}[/pgfplots/layers/Bowpark,
width=\textwidth,
xmin=-0.5, xmax=6,
ymin=-0.5, ymax=4,
axis line style = thin,
axis lines=middle,
axis line style={-{Stealth[length=2.5mm]}},
thick,
grid=major, grid style={dashed,gray!30}]

\end{axis}
\end{tikzpicture}

\end{document}


doesn't work neither (plot is above grid, but also above the axis). I used \addplot[blue, samples = 100] {x^2}; instead of \addplot[samples = 100] {x^(1/2)}; to better highlight the superposition of the plot, the grid and the axis.

What's wrong? Note that also trying

\pgfplotsset{
/pgfplots/layers/Bowpark/.define layer set={
axis background,main,axis grid,axis ticks,axis lines,axis tick labels,
axis descriptions,axis foreground
}{/pgfplots/layers/standard},
}


(switched the order of main and axis grid) produces the same output.

Maybe something in my configuration ignores the layers order?

• The easiest way is probably to use axis on top but draw the grid by hand, behind the plot. – Henri Menke Oct 26 '18 at 14:37
• @HenriMenke As far as I understood, the fact that axis and grid go together, complicates the problem. Having several plots with different axis max and min values would make quite troublesome also to draw by hand the grid each time. – BowPark Oct 26 '18 at 14:41

This is discussed at length in section 4.27 of the pgfplots manual. All I did is to define a new layer set based on axis on top in which I flip the order of axis grid and main.

\documentclass{article}
\usepackage{pgfplots}
\usetikzlibrary{shapes.geometric,arrows,arrows.meta}
\pgfplotsset{
/pgfplots/layers/Bowpark/.define layer set={
axis background,axis grid,main,axis ticks,axis lines,axis tick labels,
axis descriptions,axis foreground
}{/pgfplots/layers/standard},
}

\begin{document}

\begin{tikzpicture}
\begin{axis}[width=\textwidth,
xmin=-0.5, xmax=6,
ymin=-0.5, ymax=4,
axis line style = thin,
axis lines=middle,
axis line style={-{Stealth[length=2.5mm]}},
thick,
grid=major, grid style={dashed,gray!30},
set layers=Bowpark]

\end{axis}
\end{tikzpicture}

\end{document}


EDIT: Fixed bug, I was too sloppy, sorry!

• +1. Very good. I'm deleting my answer. With of sincerity I have not seen well the screen. – Sebastiano Oct 27 '18 at 9:41
• I suspect that something in my environment ignores the layers order. Please, check out my edited question for more details about your code. And anyway thank you – BowPark Oct 27 '18 at 13:06
• @BowPark Yes, you're right, I was too sloppy. You need to say set layers=Bowpark to activate the layers. My bad! – user121799 Oct 27 '18 at 16:13
• Its a sign of a great marmot that they tidiy up after themselves :-) – user170109 Oct 27 '18 at 16:26
• Don't worry and thank you so much. It works exactly as in your image! – BowPark Oct 27 '18 at 17:13