Pgfplots/groupplots: Accessing plots using the `axes cs` coordinate system

pgfplots

  • pgfplots offers the groupplots library (Chapter 5.8) which is great!
  • I want to draw some lines between the plots.
  • This is already possible using something like group c1r1.center etc.

enter image description here

  • Problem: I want to access the axes cs coordinate system of the plots so that I can draw lines between specific data points.
  • Question: Is it possible to draw lines between plots using the axes cs coordinate system? In the provided MWE, the following line was my attempt to do so: \draw [thick, green] (myGroupName c1r2, axis cs:0.5,0.5) node {12} -- (myGroupName c2r2.center) node {22};. This should generate the same line as the yellow line.

enter image description here


\documentclass[border = 5pt, multi = {tikzpicture}]{standalone}

\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
% pgfplots manual: 5.8 Grouping plots
\usepgfplotslibrary{groupplots}


\begin{document}

\begin{tikzpicture}[]
\begin{groupplot}[
    group style = {
        group size = 2 by 2,
        group name = myGroupName,
        },
    height = 3.5cm,
    width = 3.5cm,
    /tikz/font = \small,
    ]
%% Plots
% Plot 1
\nextgroupplot 
\addplot coordinates {(0,1) (1,0)};
% Plot 2
\nextgroupplot 
\addplot coordinates {(0,1) (1,0)};
% Plot 3
\nextgroupplot 
\addplot coordinates {(0,1) (1,0)};
% Plot 4
\nextgroupplot
\addplot coordinates {(0,1) (1,0)};
\end{groupplot}
%
%% Annotations
\draw [thick, red] (myGroupName c1r1.center) node {11} -- (myGroupName c2r1.center) node {21};
% Works
\draw [thick, yellow] (myGroupName c1r2.center) node {12} -- (myGroupName c2r2.center) node {22};
% Does NOT work
%\draw [thick, green] (myGroupName c1r2 axis cs:0.5,0.5) node {12} -- (myGroupName c2r2.center) node {22};
\end{tikzpicture}

% Taken from page 458 from the 
%   pfgplots manual (Revision 1.18.1 (2021/05/15)).
\begin{tikzpicture}[shorten >=4pt,shorten <=4pt]
\begin{groupplot}[group style={group size=2 by 2},
height=3.5cm,width=3.5cm,/tikz/font=\small]
\nextgroupplot%1
\addplot coordinates {(0,1) (1,0)};
\nextgroupplot%2
\addplot coordinates {(0,1) (1,0)};
\nextgroupplot%3
\addplot coordinates {(0,1) (1,0)};
\nextgroupplot%4
\addplot coordinates {(0,1) (1,0)};
\end{groupplot}
\draw [thick,>=latex,->,red]
(group c1r1.center) node {1.} --
(group c2r1.center) node {2.};
\draw [thick,>=latex,->,red]
(group c2r1.center) --
(group c1r2.center) node {3.};
\draw [thick,>=latex,->,red]
(group c1r2.center) --
(group c2r2.center) node {4.};
\end{tikzpicture}

\end{document}

enter image description here

Best Answer

I do not know if it is possible to access the coordinate system from the outside in a group plot. Normally it is not possible and the standard method of using named coordinates also works here:

\documentclass[border=5pt, multi={tikzpicture}]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\usepgfplotslibrary{groupplots}
\begin{document}
\begin{tikzpicture}
\begin{groupplot}[
  group style={group size=2 by 2, group name=myGroupName},
  height=3.5cm, width=3.5cm,
  /tikz/font = \small,
]
\nextgroupplot 
\addplot coordinates {(0,1) (1,0)};
\nextgroupplot 
\addplot coordinates {(0,1) (1,0)};
\nextgroupplot 
\addplot coordinates {(0,1) (1,0)};
\coordinate (a) at (0.5,0.5);
\nextgroupplot
\addplot coordinates {(0,1) (1,0)};
\coordinate (b) at (0.5,0.5);
\end{groupplot}
\draw [thick, red] (myGroupName c1r1.center) node{11} -- (myGroupName c2r1.center) node{21};
%\draw [thick, yellow] (myGroupName c1r2.center) node{12} -- (myGroupName c2r2.center) node{22};
\draw [thick, green] (a) node{12} -- (b) node{22};
\end{tikzpicture}
\end{document}

Four graphs with lines between them