4

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:

enter image description here

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:

enter image description here

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]
      \nextgroupplot[cell picture=if necessary] \addplot {1};
      \node [fill,red,circle,draw,label={above:A}] (a) at (axis cs:-4,2) {};
      \nextgroupplot[cell picture=if necessary] \addplot {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}
4
  • 1
    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?
    – nickpapior
    Mar 18, 2013 at 21:03
  • If I'm not mistaken this has a duplicate somewhere on this site...
    – percusse
    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. :)
    – nickpapior
    Mar 19, 2013 at 12:50

1 Answer 1

5

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.

enter image description here

\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]
      \addplot {1};
      \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]
      \addplot {2.5};
      \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]
      \addplot {2};
      \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]
      \addplot {1.5};
      \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}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.