# pgfplots layer issue

I'm trying to define my own layer set which enables plots above gridlines, but below the axis lines, ticks, etc. I've made the MWE based on chapter 4.27.2 of the pgfplots manual. However it's not working and I don't know whats wrong.

The plot in the MWE is above the gridline, but not below the axis lines and ticks. Moreover, the example on page 388 of the manual describing the standard layer set doesn't compile stating e.g. that /pgfplots/grid style= ... is not a known key. So what is missing here?

MWE:

\documentclass[]{article}

\usepackage{pgfplots}

\pgfplotsset{compat=1.13}

\pgfplotsset{
/pgfplots/layers/mylayer/.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}
\pgfplotsset{set layers=mylayer}%
\begin{axis}[
axis x line=bottom,
axis y line=left,
ymajorgrids = true,
]%

\end{axis}%
\end{tikzpicture}%

\end{document}


Edit:

The second MWE does what I want. However I don't want to define for every addplot on layer=axis grid. In the manual it's state that the layer main ... as it will contain every graphical element which is not associated with a specific layer. So I thought every plot should be in main. Technical it's also wrong, because the plot shouldn't be on the grid layer.

Moreover, I can't define my own layer (see MWE 3) as it fails to compile.

MWE 2:

\documentclass[]{article}

\usepackage{pgfplots}

\pgfplotsset{compat=1.13}

\pgfplotsset{
/pgfplots/layers/mylayer/.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}
\pgfplotsset{set layers=mylayer}%
\begin{axis}[
axis x line=bottom,
axis y line=left,
ymajorgrids = true,
]%

\end{axis}%
\end{tikzpicture}%

\end{document}


MWE 3:

\documentclass[]{article}

\usepackage{pgfplots}

\pgfplotsset{compat=1.13}

\pgfplotsset{
/pgfplots/layers/mylayer/.define layer set=
{axis background,axis grid,axis mymain,main,axis ticks,axis lines,axis tick labels,%
axis descriptions,axis foreground}
{
mymain style= {/pgfplots/on layer=axis mymain},}
}

\begin{document}

\begin{tikzpicture}
\pgfplotsset{set layers=mylayer}%
\begin{axis}[
axis x line=bottom,
axis y line=left,
ymajorgrids = true,
]%

\end{axis}%
\end{tikzpicture}%

\end{document}


You found a software defect: axis lines do not respect the configuration item "axis lines" in layer lists.

I accept this as bug report and will fix it until the next stable version of pgfplots (current stable is 1.13, next stable will probably be 1.14).

A workaround could be to use axis on top which is the "pre-layer" functionality to ensure that axis lines are drawn on top of the plot. Unfortunately, pgfplots is smart enough to detect that axis on top does not really make sense if set layers is active as well, and it reconfigures the layers. Here is a hack which bypasses this smart auto-deactivation and which appears to solve your problem until the next version of pgfplots comes with a bugfix:

\documentclass[]{standalone}

\usepackage{pgfplots}

\pgfplotsset{compat=1.13}

\pgfplotsset{
/pgfplots/layers/mylayer/.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}
\pgfplotsset{set layers=mylayer}%

\pgfplotsset{/dummy/workaround/.style={/pgfplots/axis on top}}
\begin{axis}[
axis x line=bottom,
axis y line=left,
axis line style={line width=1pt},
/dummy/workaround,
ymajorgrids = true,
]%


I changed your image such that it contains thicker lines and different colors for the sake of verification. The hack to activate axis on top without interfering with the layer configuration is to smuggle this style /dummy/workaround into the axis. It works because keys which do not start with /pgfplots/ are executed after the consistency check which reconfigures the layers if needed.