0

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

  1. The title is set on the axis descriptions-layer and
  2. 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.

  • It doesn't solve my issue, because although the title is in front of the grid, the grid is NOT in front of the plot (which is also the default for pgfplots). I never said you used an undefined layer, I said if I use an undefined layer … – user3284229 May 21 at 14:15
1

Well, you changed your question (and made some statements that I do not find appropriate). However, it is also true that the title is not susceptible to the layers, which can be verified by looking up the code that adds the title in pgfplots.code.tex. It is given by

\long\def\pgfplots@show@title#1{%
    \node%
            [/pgfplots/every axis title]
            {#1};
}

As you see it does not set any layer. You can fix this as follows.

\documentclass[tikz]{standalone}
\usepackage{pgfplots}

\usepgfplotslibrary{fillbetween}

\pgfplotsset{
    compat = 1.16,
}
\makeatletter
\long\def\pgfplots@show@title#1{%
\begin{pgfonlayer}{axis foreground}
        \node%
                [/pgfplots/every axis title]
                {#1};
\end{pgfonlayer}
}
\makeatother

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

enter image description here

  • As I understand the question, OP like to achieve title on the top, grid on the main and fill area together with its border lines on background layer. – Zarko May 20 at 16:54
  • @Zarko is correct. I tried my own layer in the foreground, but the title was still behind the grid lines. I think the title is set w/o observing /pgfplots/on layer in the title style. At least I didn't see anything observing a layer configuration when setting the title in the pgfplots code. – user3284229 May 21 at 6:07
  • Your answer has the plot on top of the grid. In fact, your answer is the default behaviour of pgfplots without modifying layers. I want the grid on top of the plot and the title on top of the grid. I'm not sure how to make that any more clear :( – user3284229 May 21 at 14:12
  • @user3284229 Do you agree that the original version of your question did not say this clearly? Do you agree that "marmot's answer does also not work" is an unnecessary and unfair criticism, my answer did work in the sense that it did answer what the original question appeared to be? – user121799 May 21 at 19:19

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.