为什么标题的排序与组图中数据的排序不一样?

为什么标题的排序与组图中数据的排序不一样?

我正在尝试使用以下示例显示几个图。我从pgfplots不同于groupplots数据本身的数据文件中获取每个图的标题。数据似乎按照手册指定的正确顺序绘制,但标题未应用于正确的图(例如,“0:0 - 5”应该在右上角)。似乎参数比实际值大一#1,但随后加载了正确的数据列。为什么标题的排序方式与数据不同?

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

显示错误标题位置的示例输出

答案1

\addplot我相信这与常规 TikZ/PGF 命令和和命令的评估顺序\nextgroupplot被互换有关,尽管直观上这会导致计数器太低。

无论如何,避免这种情况的一个好方法是将命令和title键放入自定义.code键中,如下所示:

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\)}
}

这样,正确的执行顺序就得以保持。你的循环看起来就像

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

相关内容