8

I use legend style={legend columns=*} to create columns in my pgfplots legends. However, this options only changes the way legend entries are displayed. What I often need is to display various data for each plot, like in a table. Here is an example of the (ugly) work-around I use so far:

\documentclass{standalone}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
title={$y=a \cdot x + b$},
xlabel=$x$,
ylabel=$y$,
legend pos=south east,
legend style={legend columns=2},
]

\addlegendimage{empty legend}
\addlegendimage{empty legend}
\addlegendentry{$\quad a \quad$} % \quad are just here to make columns wider... I suppose there's a better way
\addlegendentry{$\quad b \quad$}

\addplot+[domain=-1:2, samples=10] {3*x+1};
\addlegendimage{empty legend}
\addplot+[domain=-1:2, samples=10] {2*x+2};
\addlegendimage{empty legend}
\addplot+[domain=-1:2, samples=10] {1*x+3};
\addlegendimage{empty legend}

\addlegendentry{3}  % first data for the first graph
\addlegendentry{1}  % second data for the first graph
\addlegendentry{2}  % first data for the second graph
\addlegendentry{2}  % you've got it...
\addlegendentry{1}
\addlegendentry{3}

\end{axis}

\end{tikzpicture}
\end{document}

double entry legend

I'd be grateful for any ideas to make the syntax cleaner and, above all, robust!

Stefan Pinnow
  • 29,535

1 Answers1

9

I think the easiest solution is to create your "legend" yourself with the \matrix command. To get the \addplot styles you just need to \label them accordingly and to recall them, use the \ref feature inside a \matrix cell.

For more details please have a look at the comments in the code.

\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
    % load `matrix' library so we can use the `matrix of nodes' feature
    \usetikzlibrary{
        matrix,
    }
    % use `compat' level 1.3 (or higher) to use the advanced placement features
    % for the axis labels
    \pgfplotsset{
        compat=1.3,
    }
\begin{document}
    \begin{tikzpicture}
        \begin{axis}[
            title={$y=a \cdot x + b$},
            xlabel=$x$,
            ylabel=$y$,
        ]
            \addplot+ [domain=-1:2, samples=10] {3*x+1};
                % add labels to the plots
                \label{plot:line1}
            \addplot+ [domain=-1:2, samples=10] {2*x+2};
                \label{plot:line2}
            \addplot+ [domain=-1:2, samples=10] {1*x+3};
                \label{plot:line3}

            % create a (dummy) coordinate where we want to place the legend
            %
            % (The matrix cannot be placed inside the `axis' environment
            %  directly, because then a catcode error is raised.
            %  I guess that this is caused by the `matrix of nodes' feature)
            \coordinate (legend) at (axis description cs:0.97,0.03);
        \end{axis}

        % create the legend matrix which is placed at the created (dummy) coordinate
        % and recall the plot specification using the `\ref' command
        %
        % adapt the style of that node to your needs
        % (e.g. if you like different spacings between the rows or columns)
        \matrix [
            draw,
            matrix of nodes,
            anchor=south east,
        ] at (legend) {
                             & $a$ & $b$ \\
            \ref{plot:line1} & 3   & 1   \\
            \ref{plot:line2} & 2   & 2   \\
            \ref{plot:line3} & 1   & 3   \\
        };

    \end{tikzpicture}
\end{document}

image showing the result of above code

Stefan Pinnow
  • 29,535
  • Much cleaner indeed. Going a little bit further, I'm often using \addlegendimage{*} in my legends. With your method, I'd define a \legendimage by hand, mark it with a \label and call it in the matrix with \ref. Any ideas to make more straight foward? – user122824 Jan 12 '17 at 07:51
  • Thanks for editing my answer. To your question: I am not sure if I have understood it right, but I would define a new legend style in the preamble like line legend (have a look in the PGFPlots manual how it is defined and adapt it) and then just use this legend later in the corresponding \addplots. If I misunderstood your question, please just ask a follow-up question illustrating a bit more with an example. – Stefan Pinnow Jan 12 '17 at 08:01
  • done! I wanted to post the follow-up question first, but I didn't quite find the time this morning... Thanks for the remainder. – user122824 Jan 12 '17 at 09:46
  • You are free to do it whenever you want ... :) – Stefan Pinnow Jan 12 '17 at 09:58
  • Now that you gave me the correct keywords, I was able to find a similar question/answer: (http://tex.stackexchange.com/questions/278530/how-i-can-customize-a-legend-on-pgfplots/278538#278538). John Kormylo's syntax is slightly different, using node and tabular instead of matrix. Sounds like this question was a duplicate, with a more explicit title! – user122824 Jan 12 '17 at 14:25
  • Indeed it seems that is was a duplicate. Shall we close this one then as duplicate? – Stefan Pinnow Jan 12 '17 at 14:35
  • You know better than me the rule of the house! I've found another similar question: (http://tex.stackexchange.com/questions/127496/pgf-plots-and-a-tabular-legend). – user122824 Jan 12 '17 at 14:42
  • I've posted the follow-up question: (http://tex.stackexchange.com/questions/348375/creating-legend-images-for-tabular-legend-in-pgfplots). Hope it's more explicit! – user122824 Jan 12 '17 at 15:16