使用 pgfplots 嵌套 ifnum 条件 groupplots 吗?

使用 pgfplots 嵌套 ifnum 条件 groupplots 吗?

我想使用groupplots并设置一个变量来有条件地绘制图表。以下是最小的非工作示例:

\documentclass{article}
\usepackage{pgfplots}
\usetikzlibrary{calc}
\usepgfplotslibrary{groupplots}
\def\a{1}
\begin{document}
\begin{tikzpicture
  \begin{groupplot}[group style={group size=1 by \a},height=3cm,width=6cm]
    \ifnum\a>0{ %
    \nextgroupplot[title=One]
    \addplot coordinates {(0,0) (1,1) (2,2)};
    \ifnum\a>1{ %
    \nextgroupplot[title=Two]
    \addplot coordinates {(0,2) (1,1) (2,0)};
    \ifnum\a>2{ %
    \nextgroupplot[title=Three]
    \addplot coordinates {(0,2) (1,1) (2,1)};
    \ifnum\a>3{ %
    \nextgroupplot[title=Four]
    \addplot coordinates {(0,2) (1,1) (1,0)};
    }\fi
    }\fi
    }\fi
    }\fi
  \end{groupplot}
\node (title) at (0,0) {THE Title};
\end{tikzpicture}
\end{document}

失败原因:

! Extra }, or forgotten \endgroup.
l.24     }
          \fi

不确定是不是因为嵌套的\ifnums,还是因为\nextgroupplot;如果第一个\nextgroupplot被注释掉,错误现在是:

! Undefined control sequence.
l.11     \addplot
                  coordinates {(0,0) (1,1) (2,2)};

可以进行这种有条件的群体地块绘制吗?

答案1

删除构造中的组使用groupplot。我添加了一些缩进,并更正了小类型(}环境开头缺失tikzpicture):

在此处输入图片描述

\documentclass{article}
\usepackage{pgfplots}
\usetikzlibrary{calc}
\usepgfplotslibrary{groupplots}
\def\a{3}
\begin{document}
\begin{tikzpicture}
  \begin{groupplot}[group style={group size=1 by \a},height=3cm,width=6cm]
    \ifnum\a>0
      \nextgroupplot[title=One]
      \addplot coordinates {(0,0) (1,1) (2,2)};
      \ifnum\a>1
        \nextgroupplot[title=Two]
        \addplot coordinates {(0,2) (1,1) (2,0)};
        \ifnum\a>2
          \nextgroupplot[title=Three]
          \addplot coordinates {(0,2) (1,1) (2,1)};
          \ifnum\a>3
            \nextgroupplot[title=Four]
            \addplot coordinates {(0,2) (1,1) (1,0)};
          \fi
        \fi
      \fi
    \fi
  \end{groupplot}
\end{tikzpicture}
\end{document}

相关内容