# Tikz axis border on top, grids below

Is there an easy way to place the border box of a tikz graph on top, while keeping the grid lines behind data lines?

\begin{axis}[axis on top]


The above puts the border, x grid lines, and y grid lines. I just want the border on top, leaving the grids. The image below you'll see I have multiple fills which overlap the border, which looks bad. All I want to do is put the border on top so it covers the fills.

Thank you.

• Welcome to TeX.SX! Please help us to help you and add a minimal working example (MWE) that illustrates your problem. It will be much easier for us to reproduce your situation and find out what the issue is when we see compilable code, starting with \documentclass{...} and ending with \end{document}. – user31729 Apr 24 '15 at 11:17
• While the question is good,.. for this particular instance, if your data extends to 0.6V, would it not be better to extend your axis beyond 0.6V. – Steven B. Segletes Apr 24 '15 at 11:17
• You can define ,axis line style= {/pgfplots/on layer=axis foreground},. Or set a new layer. We need some code to start with. – user11232 Apr 24 '15 at 11:42
• If you name the axis {name=border] you can \draw (border.north east) -- (border.north west); after the \end{axis}. – John Kormylo Apr 24 '15 at 19:23
• @JohnKormylo, that did it. Thank you very much. Although it only seems to have completely solved the issue for the top of the border, there is still a bit of an overhang at the bottom for some reason, but I can live with that. – ritchie888 May 1 '15 at 20:01

Package pgfplots supports layers. From its documentation I would have expected that either

\begin{axis}[
set layers,
axis line style={on layer=axis foreground},
...
]


or

\begin{axis}[
set layers=axis on top,
grid style={on layer=axis background},
...
]


should have worked. But neither the axis lines could be moved to the foreground layer, neither the grid to the background layer. (Tested version of pgfplots is 2015/05/02 v1.12.1.)

From experiments I concluded, that the axis lines are put on the main layer below the plots. Thus the following examples successfully moved the plots to the pre main layer under the main layer:

\documentclass{article}
\usepackage{pgfplots}

\begin{document}
\begin{tikzpicture}
\begin{axis}[
set layers,
axis line style={on layer=axis foreground},% not working
width=50mm, height=50mm,
xmin=0, xmax=1,
ymin=0, ymax=1,
axis line style={line width=2mm},
grid,
grid style={line width=1mm},
]
\addplot[line width=10mm, red] coordinates {(0, .5) (1, .5)};
\addplot[line width=10mm, red] coordinates {(.5, 0) (.5, 1)};
\end{axis}
\end{tikzpicture}%
\begin{tikzpicture}
\begin{axis}[
set layers,
every axis plot/.append style={on layer=pre main},
width=50mm, height=50mm,
xmin=0, xmax=1,
ymin=0, ymax=1,
axis line style={line width=2mm},
grid,
grid style={line width=1mm},
]
\addplot[line width=10mm, red] coordinates {(0, .5) (1, .5)};
\addplot[line width=10mm, red] coordinates {(.5, 0) (.5, 1)};
\end{axis}
\end{tikzpicture}
\end{document}


Update for pgfplots 2020/02/29 v1.17

The previous example does not work with later versions of pgfplots. This is solved by defining a new layer configuration axis lines on top that moves the axis lines layer before main.

\documentclass{article}
\usepackage{pgfplots}

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

\begin{document}
\begin{tikzpicture}
\begin{axis}[
set layers=axis lines on top,
width=50mm, height=50mm,
xmin=0, xmax=1,
ymin=0, ymax=1,
axis line style={line width=2mm},
grid,
grid style={line width=1mm},
]
\addplot[line width=10mm, red] coordinates {(0, .5) (1, .5)};
\addplot[line width=10mm, red] coordinates {(.5, 0) (.5, 1)};
\end{axis}
\end{tikzpicture}
\end{document}

• This should be accepted as an answer. It does also prevent the (gray) grid lines from laying on top of (some of) the (black) box lines, which results visually in very strange uneven box line thicknesses (ref. the plot below on page 134 of the pgfplots manual for an example), especially when printed. – Christoph90 Mar 24 '18 at 8:55
• For me it does not work, the two images are printed the same way. – GiuTeX Sep 9 at 9:58
• @GiuTeX Thanks for noticing. I have updated the answer using a layer configuration to move the axis lines on top of the red plots. – Heiko Oberdiek Sep 11 at 21:08