Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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{
  compat=newest,
  every minor tick={very thin, LightGray},
  minor tick num=4,
  enlargelimits=0.02
}

\pgfplotstableread{
  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

\pgfplotstableread{
  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}{

        % Read in title
        \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}

Example output showing wrong title placement

share|improve this question

1 Answer 1

up vote 6 down vote accepted

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};
  }

leading to

\documentclass{standalone}

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

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

\pgfplotstableread{
  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

\pgfplotstableread{
  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}{

        % Read in title


        \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}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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