6

Is there a way to specify the order of layers of elements in a -axis?

In my certain case I'd like to have the legend on top of the grid, but below the actual graphs:

enter image description here

I know for this example there are easier ways to place the legend, but it needs to work also in this more complicated case.

In the same manner it would be interesting to know, if there is a way to get the red curve behind the blue one without change the order of the plots within the code.


MWE

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

\begin{document}
    \begin{tikzpicture}
    \begin{axis}[
    xmajorgrids=true,
    ymajorgrids=true,
    legend entries={Very very loooong legend entry, and a short one},
    legend cell align=left,
    legend to name=grouplegend,
    legend style={draw=none, fill opacity=0.7, text opacity = 1,row sep=10pt},
    ]
%    
    \addplot[line width = 5pt, color=blue] table {
        x y 
        1 1 
        2 2
        3 3
    };

    \addplot[line width = 5pt, color=red] table {
        x y 
        1 2 
        3 1
    };

    \coordinate (leg) at (rel axis cs:0,1);    
    
    \end{axis}
    
    \node[anchor= north west] at (leg){\pgfplotslegendfromname{grouplegend}};    
    
    \end{tikzpicture}
\end{document}

Though the answer of Gonzalo works great for a simple axis, it completely fails for grouplots.

MWE 2

\documentclass{standalone}
\usepackage{tikz}
\usepackage{pgfplots}
\usepgfplotslibrary{groupplots}
    
\begin{document}    
\begin{tikzpicture}
%\pgfplotsset{set layers=standard}
\begin{groupplot}[
group style={group name=group,
    group size=1 by 2,
    x descriptions at=edge bottom,
    y descriptions at=edge left,
    vertical sep=0.5cm,
},
xmajorgrids=true,
ymajorgrids=true,
legend entries={a very very loooooong legend entry A, Legend entry B},
legend cell align=left,
legend to name=grouplegend,
legend style={draw=none, fill opacity=0.5, text opacity = 1,row sep=-1pt,font=\scriptsize},
]

\nextgroupplot%

    \addplot[line width = 5pt, color=blue] table {
        x y 
        1 1 
        2 2
        3 3
    };
    \addplot[line width = 5pt, color=red] table {
        x y 
        1 2 
        3 1
    };

\nextgroupplot[]

    \addplot[line width = 5pt, color=blue] table {
        x y 
        1 1 
        2 2
        3 3
    };
    
    \addplot[line width = 5pt, color=red] table {
        x y 
        1 2 
        3 1
    };

\end{groupplot};
\node[anchor= north west] (leg) at (group c1r1.north west){\pgfplotslegendfromname{grouplegend}};    

\end{tikzpicture}    
\end{document}

So without %\pgfplotsset{set layers=standard} set, the grouplot looks alright,

enter image description here

but when set everything is screwed up:

enter image description here

Any reason and solution for that? Gonzalos first revision answer didn't work neither...

5

Yes, this is possible. You can use layers to place the elements in the desired order. I initially used \pgfdeclarelayer and \pgfsetlayers but, as percusse mentions in his comment, pgfplots offers set layers which gives a set of predefined layers that can be used to place elements:

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

\begin{document}
    \begin{tikzpicture}
    \begin{axis}[
    set layers=standard,
    xmajorgrids=true,
    ymajorgrids=true,
    legend entries={Very very loooong legend entry, and a short one},
    legend cell align=left,
    legend to name=grouplegend,
    legend style={draw=none, fill opacity=0.7, text opacity = 1,row sep=10pt},
    ]
%   
    \addplot[line width = 5pt, color=blue,on layer={axis foreground}] table {
        x y 
        1 1 
        2 2
        3 3
    };
    \addplot[line width = 5pt, color=red,on layer={axis descriptions}] table {
        x y 
        1 2 
        3 1
    };
    \coordinate (leg) at (rel axis cs:0,1); 

    \end{axis}
    \node[anchor= north west] at (leg){\pgfplotslegendfromname{grouplegend}};   
    \end{tikzpicture}
\end{document}

The result:

enter image description here

Update

With groupplots, some additional care has to be taken and cellpicture=true has to be used. I remember an old comment from pgfplot's creator talking about this bug in a similar context, but I couldn't find it. Apparently the bug has not been fixed yet.

\documentclass{standalone}
\usepackage{tikz}
\usepackage{pgfplots}
\usepgfplotslibrary{groupplots}

\pgfplotsset{compat=newest}
\begin{document}    
\begin{tikzpicture}
\pgfplotsset{set layers=standard,cell picture=true}
\begin{groupplot}[
group style={group name=group,
    group size=1 by 2,
    x descriptions at=edge bottom,
    y descriptions at=edge left,
    vertical sep=0.5cm,
},
xmajorgrids=true,
ymajorgrids=true,
legend entries={a very very loooooong legend entry A, Legend entry B},
legend cell align=left,
legend to name=grouplegend,
legend style={draw=none, fill opacity=0.5, text opacity = 1,row sep=-1pt,font=\scriptsize},
]

\nextgroupplot[]%

    \addplot[line width = 5pt, color=blue] table {
        x y 
        1 1 
        2 2
        3 3
    };
    \addplot[line width = 5pt, color=red] table {
        x y 
        1 2 
        3 1
    };

\nextgroupplot[]

    \addplot[line width = 5pt, color=blue] table {
        x y 
        1 1 
        2 2
        3 3
    };

    \addplot[line width = 5pt, color=red] table {
        x y 
        1 2 
        3 1
    };

\end{groupplot};
\node[anchor= north west] (leg) at (group c1r1.north west){\pgfplotslegendfromname{grouplegend}};   

\end{tikzpicture}    
\end{document}

enter image description here

| improve this answer | |
  • I think the OP meant them in this order: grid, legend, graph. But you did graph, grid, legend. :P – Alenanno Jul 16 '15 at 17:11
  • @Alenanno OK. I decided to complete the code. Is this the desired order? – Gonzalo Medina Jul 16 '15 at 17:31
  • 1
    pgfplots already has set layers key and arranges things via related keys. You can chhose any of the readily available layers to push legend up or down – percusse Jul 16 '15 at 17:36
  • Can you have a look at my edit? I'm very sorry for the inconveniences, but it's hard to post a minimal example, without knowing when its too minimal. Let me know if you consider it a new question now, then I will change it to a new one. – thewaywewalk Jul 16 '15 at 20:08
  • @thewaywewalk No need for another question. See my updated answer. – Gonzalo Medina Jul 16 '15 at 20:41
2

If you place the legend with \legend{...} instead of \node at (leg) {\pgfplotslegendfromname{grouplegend}}; there is another possibility that requires less changes in the code of your of (group-)plots in order to bring the legend below your plots. Furthermore, you can easily change the order of other elements, too.

First, you have to define a new desired layer set (see chapter 4.28 "Layers" in pgfplots.pdf or search for \pgfplotssetlayers) e.g. named legend behind plots with

    \pgfplotsset{
    layers/legend behind plots/.define layer set={
        axis background,axis grid,axis ticks,axis lines,axis tick labels,main,axis descriptions,axis foreground
    }{
        grid style= {/pgfplots/on layer=axis grid},
        tick style= {/pgfplots/on layer=axis ticks},
        axis line style= {/pgfplots/on layer=axis lines},
        label style= {/pgfplots/on layer=axis descriptions},
        %legend style= {/pgfplots/on layer=axis descriptions}, % <------ activate one of these rows
        legend style= {/pgfplots/on layer=axis tick labels}, % <------ activate one of these rows
        title style= {/pgfplots/on layer=axis descriptions},
        colorbar style= {/pgfplots/on layer=axis descriptions},
        ticklabel style= {/pgfplots/on layer=axis tick labels},
        axis background@ style={/pgfplots/on layer=axis background},
        3d box foreground style={/pgfplots/on layer=axis foreground},
    },
}

and then you can activate your layer set with set layers=legend behind plots, inside your \begin{groupplot}[...] arguments, together with cell picture=true,.

legend behind plots (groupplots)

MWE (with grouplots)

\documentclass{standalone}
\usepackage{tikz}
\usepackage{pgfplots}
\usepgfplotslibrary{groupplots}

\begin{document}    
\begin{tikzpicture}

% ### 1. define layer set ###, see pgfplots.pdf 4.28 LAYERS (search for \pgfplotssetlayers)
\pgfplotsset{
    layers/legend behind plots/.define layer set={
            axis background,axis grid,axis ticks,axis lines,axis tick labels,main,axis descriptions,axis foreground
    }{
        grid style= {/pgfplots/on layer=axis grid},
        tick style= {/pgfplots/on layer=axis ticks},
        axis line style= {/pgfplots/on layer=axis lines},
        label style= {/pgfplots/on layer=axis descriptions},
        %legend style= {/pgfplots/on layer=axis descriptions}, % <------ activate one of these rows
        legend style= {/pgfplots/on layer=axis tick labels}, % <------ activate one of these rows
        title style= {/pgfplots/on layer=axis descriptions},
        colorbar style= {/pgfplots/on layer=axis descriptions},
        ticklabel style= {/pgfplots/on layer=axis tick labels},
        axis background@ style={/pgfplots/on layer=axis background},
        3d box foreground style={/pgfplots/on layer=axis foreground},
    },
}

\begin{groupplot}[
group style={group name=group,
    group size=1 by 2,
    x descriptions at=edge bottom,
    y descriptions at=edge left,
    vertical sep=0.5cm,
},
xmajorgrids=true,
ymajorgrids=true,
% ### 2. select layer set ###
set layers=legend behind plots, 
cell picture=true,  
legend cell align=left,
legend style={at={(0,1)},anchor=north west,draw=none,fill opacity=0.5, text opacity = 1},%,row sep=-1pt,font=\scriptsize},
]

\nextgroupplot%

    \addplot[line width = 5pt, color=blue] table {
        x y 
        1 1 
        2 2
        3 3
    };
    \addplot[line width = 5pt, color=red] table {
        x y 
        1 2 
        3 1
    };

        \legend{a very very loooooong legend entry A, Legend entry B} % <--- specify legend here, works for all subplots

\nextgroupplot[]

    \addplot[line width = 5pt, color=blue] table {
        x y 
        1 1 
        2 2
        3 3
    };

    \addplot[line width = 5pt, color=red] table {
        x y 
        1 2 
        3 1
    };

\end{groupplot};

\end{tikzpicture}    
\end{document}

Update: As mentioned also in previous answers, cell picture=true, is needed here, too.

| improve this answer | |

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.