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}
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
.
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}