1

I have an interesting problem. I am trying to create two graphs using the groupplots environment, side-by-side, that show different sets of data elements with different legend entries and different markers. I'd like to combine their [disjoint] legend entries into a single common legend.

For example:

\documentclass{article}
\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=1.9}
\usepgfplotslibrary{groupplots}

\begin{document}
\begin{center}
\begin{tikzpicture}
\begin{groupplot}[
legend to name=MyLegend,
legend columns=3,
group style={group size=2 by 3,ylabels at=edge left},
ylabel style={text height=0.02\textwidth,inner ysep=0pt},
height=0.475\linewidth,width=0.475\linewidth,/tikz/font=\small]
\nextgroupplot
\addplot [color=black!90,mark=x] coordinates {
    (1, 10698) (2, 10853) (4, 17151) (8, 22366)
    (12,28016) (16,32345) (20,36076) (24,40908)
};
\addlegendentry{ABC}
\nextgroupplot
\addplot[color=black!75,mark=*] coordinates {
    (1, 17500) (2, 14600) (4, 25300) (8, 38800) 
    (12,49100) (16,58800) (20,67400) (24,75000) 
};
\addlegendentry{DEF}
\end{groupplot}
\end{tikzpicture}
\\
\ref{MyLegend}
\end{center}
\end{document}

The output is:

I would like the legend to both display ABC and DEF, along with their respective markers.

1 Answer 1

4

My answer to this question will also work if the legend entries are refering to different groupplots.

First the plots are labeled. Then the legend for the labeled plots is placed outside of the groupplots, The current bounding box and two additional defined coordinates are used to position the legend.

\documentclass[border=5mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.9}
\usepgfplotslibrary{groupplots}
\usetikzlibrary{matrix}

\begin{document}

\begin{tikzpicture}
  \begin{groupplot}[
      group style={group size=2 by 3,ylabels at=edge left},
      ylabel style={text height=0.02\textwidth,inner ysep=0pt},
      height=0.475\linewidth,width=0.475\linewidth,/tikz/font=\small
    ]
    \nextgroupplot
    \addplot [color=black!90,mark=x] coordinates {
        (1, 10698) (2, 10853) (4, 17151) (8, 22366)
        (12,28016) (16,32345) (20,36076) (24,40908)
    };\label{plots:ABC}% label for the first plot
    \coordinate (top) at (rel axis cs:0,1);% coordinate at top of the first plot
    \nextgroupplot
    \addplot[color=black!75,mark=*] coordinates {
        (1, 17500) (2, 14600) (4, 25300) (8, 38800) 
        (12,49100) (16,58800) (20,67400) (24,75000) 
    };\label{plots:DEF}% label for the second plot
    \coordinate (bot) at (rel axis cs:1,0);% coordinate at bottom of the last plot
  \end{groupplot}
  % legend
  \path (top|-current bounding box.south)--
        coordinate(legendpos)
        (bot|-current bounding box.south);
  \matrix[
      matrix of nodes,
      anchor=north,
      draw,
      inner sep=0.2em,
    ]at([yshift=-1ex]legendpos)
    { \ref{plots:ABC}& ABC&[5pt]
      \ref{plots:DEF}& DEF&[5pt]\\};
\end{tikzpicture}
\end{document}

Run twice to get

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .