5

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,
            ]%

            \addplot[gray] coordinates {(0.0,0.0)(20,1)(40,0)(100,0)};
        \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,
            ]%

            \addplot[gray,on layer=axis grid] coordinates {(0.0,0.0)(20,1)(40,0)(100,0)};
        \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,
            ]%

            \addplot[gray,on layer=axis mymain] coordinates {(0.0,0.0)(20,1)(40,0)(100,0)};
        \end{axis}%
\end{tikzpicture}%

\end{document}
7

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,
            ]%

            \addplot[red,line width=3pt] coordinates {(0.0,0.0)(20,1)(40,0)(100,0)};
        \end{axis}%
\end{tikzpicture}%

\end{document}

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.

enter image description here

| improve this answer | |
  • Thank you very much. That problem kept me busy for hours, because I thought I make something wrong. I found this answer tex.stackexchange.com/a/266650/45570 as another solution to my problem. However, because it uses the axis foreground it could cause other problems. – user2653422 Jan 17 '16 at 20:37
  • Thank you! There is, however, a slight problem with this: this also causes the ticks to appear in front of the axis lines. Especially around the origin, the result looks a bit off. – Egon Jul 16 '16 at 15:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.