# Why aren't the titles ordered in the same way data is in groupplots?

I'm trying to display a few plots using pgfplots and groupplots using the following example. I take the titles of each plot from a different data file than the data itself. The data appear to be plotted in the correct order, as the manual specifies, but the titles aren't applied to the correct plots (for example, "0: 0 - 5" should be in the upper right). It seems as if the #1 argument is one greater than it should be, but then the correct data column is loaded. Why are the titles not ordered the same way as the data?

\documentclass{standalone}

\usepackage{pgfplots}
\usepackage{pgfplotstable}
\usepgfplotslibrary{groupplots}

\pgfplotsset{
every minor tick={very thin, LightGray},
minor tick num=4,
enlargelimits=0.02
}

Time      {Data 0}  {Data 1}  {Data 2}  {Data 3}
0         7.0       2.3       0.3       1.4
1         6.0       3.6       1.2       2.7
2         5.0       4.8       3.0       3.5
3         4.0       5.9       3.9       7.3
4         3.0       7.3       4.3       6.0
5         2.0       6.5       5.5       7.2
6         1.0       8.9       7.0       8.4
}\datatable

Min  Max
0    5
6    10
11   15
20   25
}\groupmeta

\begin{document}

\begin{tikzpicture}

\begin{groupplot}[
xlabel={Time},
ylabel={Amplitude},
every axis title shift=0,
% Style list for each plot in order
cycle list={
{only marks, mark=o}
},
group style={group size=2 by 2,
xlabels at=edge bottom,
ylabels at=edge left}]

\pgfplotsinvokeforeach{0,1,2,3}{

\pgfplotstablegetelem{#1}{Min}\of{\groupmeta}
\edef\min{\pgfplotsretval}
\pgfplotstablegetelem{#1}{Max}\of{\groupmeta}
\edef\max{\pgfplotsretval}
% Just to test which element this is...
\def\test{#1}

\nextgroupplot[title=$$\test:\ \min - \max$$]
% Data
\addplot+ table[x index=0, y=Data #1] {\datatable};

}

\end{groupplot}

% Title placement
\node (dummytitle) at ($(group c1r1.north)!0.5!(group c2r1.north)$)
[above]{};
\node (title) at (dummytitle.north)
[above, yshift=\pgfkeysvalueof{/pgfplots/every axis title shift}]
{Experimental Data};

\end{tikzpicture}

\end{document}


-

I believe this has to do with the fact that the evaluation order of the general TikZ/PGF commands and the \addplot and \nextgroupplot commands are swapped around, although intuitively that should lead to the counter being one too low.

Anyway, a nice way to avoid this is to put the commands and the title key into a custom .code key like so:

set title/.code={
\pgfplotstablegetelem{#1}{Min}\of{\groupmeta}
\edef\min{\pgfplotsretval}
\pgfplotstablegetelem{#1}{Max}\of{\groupmeta}
\edef\max{\pgfplotsretval}
\edef\test{#1}
\pgfplotsset{title=$$\test:\ \min - \max$$}
}


That way, the correct execution order is maintained. Your loop would then just look like

  \pgfplotsinvokeforeach{0,1,2,3}{
\nextgroupplot[set title=#1]
\addplot+ table[x index=0, y=Data #1] {\datatable};
}


\documentclass{standalone}

\usepackage{pgfplots}
\usepackage{pgfplotstable}
\usepgfplotslibrary{groupplots}

\pgfplotsset{
every minor tick={very thin, LightGray},
minor tick num=4,
enlargelimits=0.02
}

Time      {Data 0}  {Data 1}  {Data 2}  {Data 3}
0         7.0       2.3       0.3       1.4
1         6.0       3.6       1.2       2.7
2         5.0       4.8       3.0       3.5
3         4.0       5.9       3.9       7.3
4         3.0       7.3       4.3       6.0
5         2.0       6.5       5.5       7.2
6         1.0       8.9       7.0       8.4
}\datatable

Min  Max
0    5
6    10
11   15
20   25
}\groupmeta

\begin{document}

\begin{tikzpicture}

\begin{groupplot}[
set title/.code={
\pgfplotstablegetelem{#1}{Min}\of{\groupmeta}
\edef\min{\pgfplotsretval}
\pgfplotstablegetelem{#1}{Max}\of{\groupmeta}
\edef\max{\pgfplotsretval}
\edef\test{#1}
\pgfplotsset{title=$$\test:\ \min - \max$$}
},
xlabel={Time},
ylabel={Amplitude},
every axis title shift=0,
% Style list for each plot in order
cycle list={
{only marks, mark=o}
},
group style={group size=2 by 2,
xlabels at=edge bottom,
ylabels at=edge left}]

\pgfplotsinvokeforeach{0,1,2,3}{

\nextgroupplot[set title=#1]
% Data
\addplot+ table[x index=0, y=Data #1] {\datatable};

}

\end{groupplot}

% Title placement
\node (dummytitle) at ($(group c1r1.north)!0.5!(group c2r1.north)$)
[above]{};
\node (title) at (dummytitle.north)
[above, yshift=\pgfkeysvalueof{/pgfplots/every axis title shift}]
{Experimental Data};

\end{tikzpicture}

\end{document}

-