# Ordering of legend vs zorder

Is there a way to specify the order of entries in a pgfplots legend? Right now, I can use legend entries or \addlegendentry to specify the legend label, however the legend order is the same as they appear in the tex file. Is there a way to specify the order without reordering the plots in the tex file (I would like to keep the same zorder rendering).

Thanks

• Are you using pgfplots or \datavisualization? Jun 15, 2017 at 10:39
• @Huang_d pgfplots, the datavisualization library doesn't have \addlegendentry/legend entries. Jun 15, 2017 at 10:41
• Some random order? You can reverse the order with reverse legend=true, or if you have a multicolumn legend you can transpose it with transpose legend. Otherwise you could use \label and \ref, though that is a bit more work. (There might be other ways as well, that I don't know of.) Jun 15, 2017 at 10:47
• As a follow-up: you might be able to do this the other way, i.e. change the z-order of the plots instead of the order of the legend. See tex.stackexchange.com/questions/20425/z-level-in-tikz I haven't tested this, but do note that pgfplots defines a number of layers by default (see section 4.27.2 Using predefined layers in the manual), so you might have to take that into account. Jun 26, 2017 at 13:27

Demonstrating a couple of possibilities. The last is the most flexible, but also involves the most work.

\documentclass[border=5mm]{standalone}
\usepackage{pgfplots}
\usetikzlibrary{matrix}
\begin{document}
\begin{tikzpicture}
\begin{axis}[reverse legend]

\legend{$x$,$-x$,$2x$,$-2x$}
\end{axis}
\end{tikzpicture}
\begin{tikzpicture}
\begin{axis}[legend columns=2,transpose legend]

\legend{$x$,$-x$,$2x$,$-2x$}
\end{axis}
\end{tikzpicture}
\begin{tikzpicture}
\begin{axis}[name=aa]
\label{a}
\label{b}
\label{c}
\label{d}

\end{axis}
\matrix [
matrix of nodes,
nodes={anchor=west},
anchor=north east,
at={([shift={(-3pt,-3pt)}]aa.north east)},
fill=white,
draw,
inner sep=2pt,
row sep=2pt
] {
\ref{b} $-x$ \\
\ref{a} $x$ \\
\ref{d} $-2x$ \\
\ref{c} $2x$ \\
};

\end{tikzpicture}
\end{document}

• This definitely works, thought it's disappointing that it is a bit complicated to do something that should be very standard Jun 15, 2017 at 13:26

Section 3.3.3 of the pgfplots manual says:

You might wonder how the text labels map to \addplot instructions. Well, they are mapped by index. The first label is assigned to the first plot, the second label to the second plot and so on. You can exclude plots from this counting if you add the forget plot option to the plot (using \addplot+[forget plot], for example). Such plots are excluded from both cycle lists and legends.

The second ingredient came from this answer about adding a secon y-axis to a plot. There, a second axis environment (having the y-axis on the right) is placed just over the first one, and labels are used to import the plots from the first axis environment to the legend in the second one. When the labels are added to the second axis environment, they get enumerated just like a plot would. But, the labels are referenced by name, we can add them in any order we want.

To create a label, just add

\label{foobar}


after adding the plot.

And reference it with

\addlegendimage{/pgfplots/refstyle=foobar}


So far, this is simple. Now, we don't want the second axis environment to be seen, so we declare it like

\begin{axis}[ axis x line=none, axis y line=none,] % Hide the plot


And one more thing: pgfplots will create an error, if there is no plot in the axis environment. So, we add one, but make it invisible:

\addplot[opacity=0] {0};


Now, we can add our legend, and provide an empty entry for the invisible plot:

\legend{,foo,bar,baz}


OR, as a second and more comfortable option, we add the invisible plot with the forget plot option:

\addplot[opacity=0,forget plot] {0};


And then add our labeled plots directly with the pertaining legend entry:

\addlegendimage{/pgfplots/refstyle=foobar}  \addlegendentry{This is plot foobar}


Using Torbjørn T.'s example, I came up with the following:

\documentclass[border=5mm]{standalone}
\usepackage{pgfplots}
\usetikzlibrary{matrix}
\begin{document}
\begin{tikzpicture} % The original example, legend in order
\begin{axis}
\legend{$x$,$-x$,$2x$,$-2x$}
\end{axis}
\end{tikzpicture}
\begin{tikzpicture} % First option. Works, but not very comfortable
\begin{axis}
\end{axis}
\begin{axis}[ axis x line=none, axis y line=none,] % Hide the plot
\addplot[opacity=0] {0}; % There must be *something* in the plot
\legend{,$-x$,$-2x$,$2x$,$x$} % Note the leading comma, meaning there should be no index for the first (invisible) plot
\end{axis}
\end{tikzpicture}
\begin{tikzpicture} % The best option, imho.
\begin{axis}
\addlegendimage{/pgfplots/refstyle=plot:two}  \addlegendentry{$-x$}
\addlegendimage{/pgfplots/refstyle=plot:four} \addlegendentry{$-2x$}
\addlegendimage{/pgfplots/refstyle=plot:three}\addlegendentry{$2x$}
\addlegendimage{/pgfplots/refstyle=plot:one}  \addlegendentry{$x$}