9

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.

As specified in this answer,

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}]

    \addplot[blue, samples = 100] {x^2};

\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.

enter image description here

enter image description here

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?

BowPark
  • 1,213
  • 1
    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
  • 1
    @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

1 Answers1

12

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]

\addplot[blue, samples = 100] {x^2};

\end{axis} \end{tikzpicture}

\end{document}

enter image description here

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


Edit 2022-10-30 by User Manuel Kuehner

If you use the fillbetween library, it seems you must also include the pre main in the list. In my case, I put pre main it before main

Package pgfplots Warning: 'fill between': Could not activate graphics layer 'pre main'. Filled path will be on top of the other ones. Please ensure that 'pre main' is somewhere in the layer list (or set '/tikz/fill between/on layer=').


  • +1. Very good. I'm deleting my answer. With of sincerity I have not seen well the screen. – Sebastiano Oct 27 '18 at 09: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
  • 1
    @BowPark Yes, you're right, I was too sloppy. You need to say set layers=Bowpark to activate the layers. My bad! –  Oct 27 '18 at 16:13
  • 1
    Its a sign of a great marmot that they tidiy up after themselves :-) –  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