Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I would like to place a legend outside an axis using symbols from multiple plots within a groupplot. Trying it the way I show below doesn't seem to work, just the last entry is shown. How can this be done in a way such that all entries are shown?

\documentclass[crop]{standalone}

\usepackage[svgnames]{xcolor}
\usepackage{pgfplots}
\usepgfplotslibrary{groupplots}

\begin{document}

  \begin{tikzpicture}
    \begin{groupplot}[
      group style={
        {group size=2 by 2}},
      legend to name=grouplegend]

      \nextgroupplot[title=One]
      \addplot+[color=Blue] coordinates {(0,0) (1,1) (2,2)};
      \addlegendentry{A}

      \nextgroupplot[title=Two]
      \addplot+[color=Red] coordinates {(0,2) (1,1) (2,0)};
      \addlegendentry{B}

      \nextgroupplot[title=Three]
      \addplot+[color=Gray] coordinates {(0,2) (1,1) (2,1)};
      \addlegendentry{C}

      \nextgroupplot[title=Four]
      \addplot+[color=Orange] coordinates {(0,2) (1,1) (1,0)};
      \addlegendentry{D}

    \end{groupplot}

    \node (l1) at ($(group c1r2.south)!0.5!(group c2r2.south)$)
      [below, yshift=-2\pgfkeysvalueof{/pgfplots/every axis title shift}]
      {\ref{grouplegend}};

  \end{tikzpicture}

\end{document}

four plots in a 2x2 grid showing the legend with only the last entry

share|improve this question
    
I think you're best off posting a feature request for this on Sourceforge. PGFplots is being very actively developed, so there's a good chance of this being implemented relatively fast. –  Jake Jun 19 '12 at 7:14
add comment

1 Answer

You can use the /pgfplots/legend to name={<name>} feature described in the manual reference under "Legends outside of an axis". The example quoted is:

% Preamble: \pgfplotsset{width=7cm,compat=1.3}
\pgfplotsset{footnotesize,samples=10}
\begin{center}% note that \centering uses less vspace...
\begin{tikzpicture}
\begin{axis}[
legend columns=-1,
legend entries={$(x+0)^k$;,$(x+1)^k$;,$(x+2)^k$;,$(x+3)^k$},
legend to name=named,
title={$k=1$}]
\addplot {x};
\addplot {x+1};
\addplot {x+2};
\addplot {x+3};
\end{axis}
\end{tikzpicture}
%
\begin{tikzpicture}
\begin{axis}[title={$k=2$}]
\addplot {x^2};
\addplot {(x+1)^2};
\addplot {(x+2)^2};
\addplot {(x+3)^2};
\end{axis}
\end{tikzpicture}
%
\begin{tikzpicture}
\begin{axis}[title={$k=3$}]
\addplot {x^3};
\addplot {(x+1)^3};
\addplot {(x+2)^3};
\addplot {(x+3)^3};
\end{axis}
\end{tikzpicture}
\\
\ref{named}
\end{center}

Which produces:

one legend to rule them all

Edit: I guess this doesn't work for group plots. Here's a horrible hack that kinda works:

\documentclass[crop]{standalone}

\usepackage[svgnames]{xcolor}
\usepackage{pgfplots}
\usepgfplotslibrary{groupplots}

\begin{document}

  \begin{tikzpicture}
    \begin{groupplot}[
      group style={
        {group size=2 by 2}},
        xmin=0,
        enlarge x limits=true,
      ]

      \nextgroupplot[title=One, legend to name=grouplegend]
      \addplot+[color=Blue] coordinates {(0,0) (1,1) (2,2)};
      \addlegendentry{A}
      \addplot+[color=Red] coordinates {(-1,0)};
      \addlegendentry{B}
      \addplot+[color=Gray] coordinates {(-1,0)};
      \addlegendentry{C}
      \addplot+[color=Orange] coordinates {(-1,0)};
      \addlegendentry{D}

      \nextgroupplot[title=Two]
      \addplot+[color=Red] coordinates {(0,2) (1,1) (2,0)};

      \nextgroupplot[title=Three]
      \addplot+[color=Gray] coordinates {(0,2) (1,1) (2,1)};

      \nextgroupplot[title=Four]
      \addplot+[color=Orange] coordinates {(0,2) (1,1) (1,0)};

    \end{groupplot}

    \node (l1) at ($(group c1r2.south)!0.5!(group c2r2.south)$)
      [below, yshift=-2\pgfkeysvalueof{/pgfplots/every axis title shift}]
      {\ref{grouplegend}};

  \end{tikzpicture}

\end{document}

This produces:

yay for hacks

share|improve this answer
    
If I understand the question correctly, this won't work for this case: You can't "collect" plots from different axes using this method. In the question, each axis contains one plot, but the legend should contain four entries (blue, red, gray and orange). –  Jake Jun 15 '12 at 17:23
    
Ah, I see, d'oh. I've edited the answer with an idea to rectify the situation. –  slurms Jun 15 '12 at 18:02
    
@Jake is correct. I will look at the edit to see if it works. –  sappjw Jun 18 '12 at 13:34
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.