4

I want to place the legend to the right of groupplots using the pgfplots package, and for the legend to be vertically aligned at the center. In other words, I want the legend to be placed at the point where it says "Put the legend here".

Question: How do I do this?

\documentclass[margin=2mm]{standalone}

\usepackage{pgfplots}
\usepgfplotslibrary{groupplots}

\pgfplotsset{compat=newest}

\begin{document}
\begin{tikzpicture}[shorten >=4pt,shorten <=4pt]
  \begin{groupplot}[
    group style={
      group size=2 by 2,
      x descriptions at=edge bottom,
      y descriptions at=edge left,
    },
    height=3.5cm,width=3.5cm,/tikz/font=\small,
    xlabel=time $t$ / h,
    ylabel=$c$ / mol/L,
    ]
    \nextgroupplot% 1
    \addplot coordinates {(0,1) (1,0)};
    \addplot coordinates {(0,0) (1,1)};
    \nextgroupplot% 2
    \addplot coordinates {(0,1) (1,0)};
    \addplot coordinates {(0,0) (1,1)};
    \nextgroupplot% 3
    \addplot coordinates {(0,1) (1,0)};
    \addplot coordinates {(0,0) (1,1)};
    \nextgroupplot% 4
    \addplot coordinates {(0,1) (1,0)};
    \addplot coordinates {(0,0) (1,1)};
  \end{groupplot}
  \node [right=5mm,anchor=west] at
    ($(group c2r1.south east)!0.5!(group c2r2.north east)$) {Put the legend here};
\end{tikzpicture}
\end{document}

enter image description here

2 Answers 2

4

To add legend to a place to the right of the plots, one needs to add a legend style to the groupplot environment where at=() is the key. The (0,0) means the lower left of the corner while (1,1) means the upper right corner. Such system is called axis description cs documented on page 103.

[legend style={at={(1.03,1.4)}, anchor=north west}

enter image description here

Code:

\documentclass[margin=2mm]{standalone}

\usepackage{pgfplots}
\usepgfplotslibrary{groupplots}

\pgfplotsset{compat=newest}

\begin{document}
\begin{tikzpicture}[shorten >=4pt,shorten <=4pt]
  \begin{groupplot}[
    group style={
      group size=2 by 2,
      x descriptions at=edge bottom,
      y descriptions at=edge left,
    },
    height=3.5cm,width=3.5cm,/tikz/font=\small,
    xlabel=time $t$ / h,
    ylabel=$c$ / mol/L,
    ]
    \nextgroupplot% 1
    \addplot coordinates {(0,1) (1,0)};
    \addplot coordinates {(0,0) (1,1)};
    \nextgroupplot% 2
    \addplot coordinates {(0,1) (1,0)};
    \addplot coordinates {(0,0) (1,1)};
    \nextgroupplot% 3
    \addplot coordinates {(0,1) (1,0)};
    \addplot coordinates {(0,0) (1,1)};
    \nextgroupplot[legend style={at={(1.03,1.4)}, anchor=north west}]% 4
    \addplot coordinates {(0,1) (1,0)};
    \addplot coordinates {(0,0) (1,1)};
\legend{Put the legend here};
  \end{groupplot}
%  \node [right=5mm,anchor=west] at
%    ($(group c2r1.south east)!0.5!(group c2r2.north east)$) {}
\end{tikzpicture}
\end{document}

Update: The OP has a follow-up and this update adds more info to it. This is the principle for groupplot. The \nextgroupplot[<axis options>] option are the options that are supplied to the following axes until the next \nextgroupplot command is seen by TEX. The order in which figures are typeset in a zigzag order, meaning the right-most figures are the 3rd, 6th and 9th figure. Since the OP wants legends to be vertically center, this solution uses the 9th figure as the reference point and specifies the coordinateat=(<x,y>). The axis description cs remains valid. Just be aware that for outer legends x>1 and y>1.

enter image description here

Code

\documentclass[margin=2mm]{standalone}

\usepackage{pgfplots}
\usepgfplotslibrary{groupplots}

\pgfplotsset{compat=newest}

\begin{document}
\begin{tikzpicture}[shorten >=4pt,shorten <=4pt]
  \begin{groupplot}[
    group style={
      group size=3 by 3,
      x descriptions at=edge bottom,
      y descriptions at=edge left,
    },
    height=3.5cm,width=3.5cm,/tikz/font=\small,
    xlabel=time $t$ / h,
    ylabel=$c$ / mol/L,
    ]
    \nextgroupplot% 1
    \addplot coordinates {(0,1) (1,0)};
    \addplot coordinates {(0,0) (1,1)};
    \nextgroupplot% 2
    \addplot coordinates {(0,1) (1,0)};
    \addplot coordinates {(0,0) (1,1)};
    \nextgroupplot% 3
    \addplot coordinates {(0,1) (1,0)};
    \addplot coordinates {(0,0) (1,1)};
    \nextgroupplot% 4
    \addplot coordinates {(0,1) (1,0)};
    \addplot coordinates {(0,0) (1,1)};
    \nextgroupplot% 5
    \addplot coordinates {(0,1) (1,0)};
    \addplot coordinates {(0,0) (1,1)};
    \nextgroupplot% 6
    \addplot coordinates {(0,1) (1,0)};
    \addplot coordinates {(0,0) (1,1)};
    \nextgroupplot% 7
    \addplot coordinates {(0,1) (1,0)};
    \addplot coordinates {(0,0) (1,1)};
    \nextgroupplot% 8
    \addplot coordinates {(0,1) (1,0)};
    \addplot coordinates {(0,0) (1,1)};
    \nextgroupplot[legend style={at={(1.1,2.2)}, anchor=north west}]% 9   
    \addplot coordinates {(0,1) (1,0)};
    \addplot coordinates {(0,0) (1,1)};
    \legend{Put the legend here};
  \end{groupplot}
%  \node [right=5mm,anchor=west] at
%    ($(group c2r1.south east)!0.5!(group c2r2.north east)$) {}
\end{tikzpicture}
\end{document}
3
  • This solution works for this case, but due to the magic numbers (1.03,1.4) it suggests to me that it is fragile and depends on the exact dimensions of the plots and the size of the legend box. That is if the height changed or if the legend box had more rows the alignment would be negatively affected. Is there a solution that works independently of the dimensions and the size of the legend box? Commented Jan 9, 2014 at 23:29
  • @ILiketoCode -- Please see my update.
    – Jesse
    Commented Jan 10, 2014 at 1:26
  • I am slightly dissatisfied with the solution because the at={(x,y)} depends on the number of plots and the size of the plots, so some manual tweaking of (x,y) is necessary. Nevertheless, I've accepted the answer. Thanks for your attempt! Commented Jan 11, 2014 at 16:31
0

All credits goes to GPT4 which gave me the answer when I tried to to the same for some plot of mine, but here is a clean and generic way of doing this using the legend to name axis parameter and pgfplotslegendfromname node attribute.

Basically, legend to name creates a custom key for your legend, which can then be used with the command \pgfplotslegendfromname{my_custom_legend_name} in a node directive. Here is how it goes:

\documentclass[margin=2mm]{standalone}

\usepackage{pgfplots}
\usepgfplotslibrary{groupplots}

\pgfplotsset{compat=newest}

\begin{document}
\begin{tikzpicture}[shorten >=4pt,shorten <=4pt]
  \begin{groupplot}[
    group style={
      group size=2 by 2,
      x descriptions at=edge bottom,
      y descriptions at=edge left,
    },
    height=3.5cm,width=3.5cm,/tikz/font=\small,
    xlabel=time $t$ / h,
    ylabel=$c$ / mol/L,
    ]
    \nextgroupplot% 1
    \addplot coordinates {(0,1) (1,0)};
    \addplot coordinates {(0,0) (1,1)};
    \nextgroupplot% 2
    \addplot coordinates {(0,1) (1,0)};
    \addplot coordinates {(0,0) (1,1)};
    \nextgroupplot% 3
    \addplot coordinates {(0,1) (1,0)};
    \addplot coordinates {(0,0) (1,1)};
    \nextgroupplot[ % 4
    legend to name=mylegend, ]% <-- HERE you set your custom legend name
    \addplot coordinates {(0,1) (1,0)};
    \addplot coordinates {(0,0) (1,1)};
    \legend{Blue, Red};
  \end{groupplot}
  \node [right=5mm,anchor=west] at
    ($(group c2r1.south east)!0.5!(group c2r2.north east)$)
    {\pgfplotslegendfromname{mylegend}}; % <-- HERE you use it to place the legend at custom coordinates
\end{tikzpicture}
\end{document}

Which yields:

enter image description here

Note: this only takes the legend of the subplot into which you used the \legend command (or possibly \addlegendentry), I do not know if there is a way to "agglomerate" legend entries coming from multiple subplots.

You must log in to answer this question.

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