pgfplot 图例位于网格顶部,但在轴和 groupplot 中的图形下方

pgfplot 图例位于网格顶部,但在轴和 groupplot 中的图形下方

有没有办法指定元素层的顺序-轴?

在我的特定情况下,我希望将图例放在网格的顶部,但位于实际图表的下方:

在此处输入图片描述

我知道对于这个例子来说,有更简单的方法来放置图例,但它也需要在这个更复杂的情况下发挥作用。

同样,如果有办法让红色曲线位于蓝色曲线后面,那也很有趣不改变情节顺序在代码中。


平均能量损失

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

虽然贡萨洛的回答对于简单的轴来说效果很好,但对于集体地块

微波辐射计 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}

因此,没有%\pgfplotsset{set layers=standard}设置,grouplot 看起来还不错,

在此处输入图片描述

但当设置好后,一切都搞砸了:

在此处输入图片描述

有什么原因和解决办法吗?Gonzalos 的第一次修订答案也没有用...

答案1

是的,这是可能的。您可以使用图层将元素按所需顺序放置。我最初使用\pgfdeclarelayer\pgfsetlayers,但是,敲击提及his commentpgfplots提供set layers一组可用于放置元素的预定义图层:

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

结果:

在此处输入图片描述

更新

使用 时groupplots,必须采取一些额外的措施,并且cellpicture=true必须使用。我记得 的pgfplot创建者在类似的上下文中谈到过这个错误,但我找不到它。显然这个错误还没有被修复。

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

在此处输入图片描述

答案2

如果将图例放置在\legend{...}而不是 中,则 \node at (leg) {\pgfplotslegendfromname{grouplegend}};还有另一种可能性,即只需对(组)图的代码进行较少的更改,即可将图例置于图的下方。此外,您还可以轻松更改其他元素的顺序。

首先,您必须定义一个新的所需图层集(参见 pgfplots.pdf 中的第 4.28 章“图层”或搜索\pgfplotssetlayers),例如命名legend behind plots

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

set layers=legend behind plots,然后,您可以使用参数中的激活图层集\begin{groupplot}[...],以及cell picture=true,

情节背后的传奇(群体情节)

MWE(与团体批次)

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

更新:正如前面的答案中所提到的,cell picture=true,这里也需要。

相关内容