4

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 cs to specify the y coordinate, and an intersection like -| (current page.center) for the x?

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}

enter image description here

jja
  • 1,813
  • 1
    You might want to take a look at http://tex.stackexchange.com/questions/37568/colors-and-legend-in-groupplots-barplot, where the legend is saved as a reference and placed using a node after the groupplots environment is complete. – Jake Feb 25 '15 at 11:41
  • 1
    Ah, that's the stuff! I couldn't find that before. This question pales in comparison. Should I still leave it up as a sort of redirect? I feel this title is easier to understand. :/ – jja Feb 25 '15 at 12:09
  • Do you reckon this is a duplicate? I feel like your question goes further than that, because you're (rightly) asking for a more automated solution that doesn't require manually choosing the right values for group c2r1. Maybe you could edit your question to ask about automating that other approach? – Jake Feb 25 '15 at 14:30
  • Hmm... yeah, I see the difference. I'll try to edit the question when I have some time. – jja Feb 27 '15 at 12:25
  • once I compile this in my texstudio.. I got the error message something that " Choice '1.11' unknown in key '/pgfplots/compat/empty l ine'. I am going to ignore this key."...how we will rectify this..@jja @ – David Jul 28 '15 at 15:05
  • For the record: I added a feature-request to the PGFPlots Tracker. – Stefan Pinnow Nov 13 '16 at 18:20

0 Answers0