This question deals with centered legend placement in a set of plots aligned with groupplots. However, as the OP stated in a comment, the (accepted) solution requires some hand tweaking.
I suspect there might be a better way to do this, maybe using the nodes per-plot that groupplots is generating as group cxry, where x is the number of the column and y is the number of the row.
However, those nodes don't exist until (I imagine?) groupplots has finished plotting (correct me if I'm wrong!). And by that time the legend has already been plotted.
I've thought of a couple of possibilities, but none seem to work:
Maybe using something like
($(group c1r1)!.5!(group c2r1)$)?Or using the
axis csto specify theycoordinate, and an intersection like-| (current page.center)for thex?
Ideally, you could specify a group legend at={(0.5,1)} or maybe something like what positioning does for a group legend at=below plot or something like that.
I understand that, short of that, I will always need some sort of knowledge of the number of plots, so I can decide if it's group c2r1 or group c3r1, but at this point, it's more the centering between those that bothers me.
The MWE below shows this: I can automatically place a node where I want the legend, and I can manually place the legend relatively close to where that node is. But I want to know if there's a way to automatically place the legend there.
\documentclass{article}
\usepackage{pgfplots}
\usetikzlibrary[pgfplots.groupplots,calc]
\pgfplotsset{
compat=1.11,
horizontal legend/.style={
every axis legend/.append style={
at={(0.2,1.18)}, anchor=south east, draw=none, font=\footnotesize,
draw=none,
/tikz/every even column/.append style={column sep=0.3cm},
},
legend cell align=left,
legend columns=4,
},
}
\begin{document}
\begin{figure}
\centering
\begin{tikzpicture}
\begin{groupplot}[
small,
group style={group size=2 by 1},
]
\nextgroupplot[]
\addplot[red] [domain=0:2*pi] {sin(deg(x)};
\addplot[blue] [domain=0:2*pi] {cos(deg(x)};
\nextgroupplot[horizontal legend]
\addplot[red] [domain=0:pi] {sin(deg(x)};
\addlegendentry{sin}
\addplot[blue] [domain=0:pi] {cos(deg(x)};
\addlegendentry{cos}
\end{groupplot}
\path ($(group c1r1.north)!.5!(group c2r1.north)$) ++ (0,1) node {HERE};
\end{tikzpicture}
\end{figure}
\end{document}
\documentclass{article}
\usepackage{pgfplots}
\usetikzlibrary[pgfplots.groupplots,calc]
\pgfplotsset{
compat=1.11,
horizontal legend/.style={
every axis legend/.append style={
at={(0.2,1.18)}, anchor=south east, draw=none, font=\footnotesize,
draw=none,
/tikz/every even column/.append style={column sep=0.3cm},
},
legend cell align=left,
legend columns=4,
},
}
\begin{document}
\begin{figure}
\centering
\begin{tikzpicture}
\begin{groupplot}[
small,
group style={group size=2 by 1},
]
\nextgroupplot[]
\addplot[red] [domain=0:2*pi] {sin(deg(x)};
\addplot[blue] [domain=0:2*pi] {cos(deg(x)};
\nextgroupplot[horizontal legend]
\addplot[red] [domain=0:pi] {sin(deg(x)};
\addlegendentry{sin}
\addplot[blue] [domain=0:pi] {cos(deg(x)};
\addlegendentry{cos}
\end{groupplot}
\path ($(group c1r1.north)!.5!(group c2r1.north)$) ++ (0,1) node {HERE};
\end{tikzpicture}
\end{figure}
\end{document}

groupplotsenvironment is complete. – Jake Feb 25 '15 at 11:41group c2r1. Maybe you could edit your question to ask about automating that other approach? – Jake Feb 25 '15 at 14:30