I want to color an area between two plots using the fillbetween library. Additionally, I want my grid lines on top of the plot (i.e. "axis on top", like in pgfplots' area style; I can't use area style because my plots don't extend down to zero :/)
Anyway, I have that all set up, but now the plot title (which I shifted downward) is somehow behind the grid lines of the plot (which I don't want).
Ok, I think it's clear what I want. Here is my boiled down NWME:
\documentclass[tikz]{standalone}
\usepackage{pgfplots}
\usepgfplotslibrary{fillbetween}
\pgfplotsset{
compat = 1.16,
}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
axis on top, % I want grid lines on top of the plot, but the title in front of the grid lines
title = {XXXXXXXXXXXXXXXXXXXXX},
grid = major,
major grid style = {
blue, line width = 5pt,
},
every axis title shift = {-10ex}, % shift title into plot area
]
\addplot[name path = A, line width = 5pt, color=red] table {
x y
1 1
2 2
3 3
};
\addplot[name path = B, line width = 5pt, color=red] table {
x y
1 1
2 2
3 3
};
\addplot fill between[of = A and B];
\end{axis}
\end{tikzpicture}
\end{document}
I've tried defining my own layers, setting title style = { /pgfplots/on layer = }
, setting the layer of the grid to a new layer, I defined, which is most foreground layer, but nothing worked.
I think the title is simply not set to the correct layer. Chapter 4.27 of the pgfplots manual (p. 410) says title style = { /pgfplots/on layer = axis descriptions }
suggesting
- The title is set on the
axis descriptions
-layer and - the layer can actually be set via title style.
But even when I set the title style layer to some nonsense non-existing layer like so: title style = { /pgfplots/on layer = herebedragons }
, Latex does not complain that the layer does not exist, which it usually does; for example, if I set that invalid layer in the major grid style
.
Hm. I'm at the end of my Latex/PGF/…-Foo :/ I'm probably missing something obvious. Have you guys any ideas, how I can get the grid in front of the plots and the title in front of the grid lines, when using fill between
(everything works w/o fill between
, btw.)?
Edit: To clarify my desired stack up is as follow (from bottom to top, with layer name in parenthesis):
fill area ("pre main"), plot ("main"), grid lines ("axis grid"), title ("axis descriptions")
I tried declaring my own layer and changing the layer stack up as described in pgfplots manual chap. 4.27. The result is always that either
- the grid is on front of the plot (desired) and the title (not wanted) or
- the title is in front of the grid (desired) but the plot is also in front of the grid (not wanted)
I hope that helps understanding the my issue better.
I have a second example, where I set a node manually on the axis descriptions
layer. In this case it is set above the grid lines (expected outcome). According to the documentation the title should also be set on the axis descriptions
layer, but it doesn't work :'(
\documentclass[tikz]{standalone}
\usepackage{pgfplots}
\usepgfplotslibrary{fillbetween}
\pgfplotsset{
compat = 1.16,
}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
axis on top, % I want grid lines on top of the plot, but the title in front of the grid lines
title = {XXXXXXXXXXXXXXXXXXXXX},
grid = major,
major grid style = {
blue, line width = 5pt,
},
every axis title shift = {-10ex}, % shift title into plot area
]
\addplot[name path = A, line width = 5pt, color=red] table {
x y
1 1
2 2
3 3
};
\addplot[name path = B, line width = 5pt, color=red] table {
x y
1 1
2 2
3 3
};
\addplot fill between[of = A and B];
\begin{pgfonlayer}{axis descriptions}
\node[anchor = north] at (axis description cs: 0.5,0.7) {FAKE TITLE ABOVE GRID};
\end{pgfonlayer}
\end{axis}
\end{tikzpicture}
\end{document}
For fun and kicks I also added the following line to the plot:
title style = {/pgfplots/on layer = {thislayerdoesnotexist}},
PGF complains about non-existing layers, but in this case, it doesn't. Which leads me to believe a layer set in the title style is ignored by pgfplots, and hence this is the reason @marmot's answer does also not work.