# External coordinates breaks alignment of groupplots

This answer showed how to make overlay drawings with nodes defined in previous plots using TikZ. However, that solution (ie. adding cell picture=if necessary to the axis) does not work with groupplots.

If I do not use the option, I get the same problem that was solved by the answer linked to above, and even worse results for the second plot:

Adding the option to the \nextgroupplot commands (which are equivalent the axis statements) solves part of the problem (the arrows point to the nodes) but it breaks alignment of the plots:

Question: how can I make overlay drawings using nodes that have been defined in previous plots by groupplots?

## MWE

\documentclass{article}
\usepackage{pgfplots}
\usetikzlibrary[pgfplots.groupplots]

\begin{document}

\begin{figure}
\begin{tikzpicture}[remember picture]
\begin{groupplot}[group style={group size=2 by 1},ymin=0,ymax=3]
\node [fill,red,circle,draw,label={above:A}] (a) at (axis cs:-4,2) {};
\node [fill,blue,circle,draw,label={above:B}] (b) at (axis cs:4,1) {};
\end{groupplot}
\end{tikzpicture}
\caption{A \texttt{groupplots} plot}
\end{figure}

This arrow should point to A
\tikz[remember picture,overlay] \draw[red,->] (0,0) -- (a);
and this arrow should point to B
\tikz[remember picture,overlay] \draw[red,->] (0,0) -- (b);

\end{document}

• it is the lag of interaction between the pgfplots layer and groupplots. Instead, you should try the "manual" alignment (there is an example of what groupplots do in its section) and see if the problem persists. Perhaps that could help? Mar 18, 2013 at 21:03
• If I'm not mistaken this has a duplicate somewhere on this site... Mar 19, 2013 at 1:05
• @zeroth, that was really helpful. I used your comment for a self-provided answer below. Hope that's OK.
– jja
Mar 19, 2013 at 12:24
• @jja That is PERFECT! Very good! Hope to see you again. :) Mar 19, 2013 at 12:50

Based on the comment by @zeroth, I was able to put together a manually aligned set of plots. I'm pretty sure the code can be improved (particularly the placement of the figure caption) and I'm not at all sure what problems this might have (with, for instance, externalize), but this solves my problem.

\documentclass{article}
\usepackage{pgfplots}

\pgfplotsset{
every axis/.append style={ymin=0,ymax=3,cell picture=if necessary}
}

\begin{document}

\begin{figure}
\begin{tikzpicture}[baseline,remember picture]
\begin{axis}[name=left column]
\node [fill,red,circle,draw,label={above:A}] (a) at (axis cs:-4,2) {};
\end{axis}
\begin{axis}[at={(left column.below south west)},yshift=-1cm,anchor=north west]
\node [fill,red,circle,draw,label={above:C}] (c) at (axis cs:4,1.5) {};
\end{axis}
\end{tikzpicture}
\hspace{0.15cm}
\begin{tikzpicture}[baseline,remember picture]
\begin{axis}[name=right column]
\node [fill,red,circle,draw,label={above:B}] (b) at (axis cs:4,1) {};
\end{axis}
\begin{axis}[at={(right column.below south west)},yshift=-1cm,anchor=north west]
\node [fill,red,circle,draw,label={above:D}] (d) at (axis cs:-4,2.5) {};
\end{axis}
\end{tikzpicture}
\caption{A fake \texttt{groupplots} plot}
\end{figure}

This arrow should point to A
\tikz[remember picture,overlay] \draw[red,->] (0,0) -- (a);
; this arrow should point to B
\tikz[remember picture,overlay] \draw[red,->] (0,0) -- (b);
; this arrow should point to C
\tikz[remember picture,overlay] \draw[red,->] (0,0) -- (c);
; and this to D
\tikz[remember picture,overlay] \draw[red,->] (0,0) -- (d);

\end{document}