跳到正文

用 groupplots 把多个图表排成网格

加载 groupplots 库,打开一个带 group style={group size=2 by 1} 的 groupplot 环境,并用 \nextgroupplot 开始每个图表。

两个并排的图表

groupplots 库把多个坐标轴排成网格,大小相同,边框对齐。group size=2 by 1 表示两列一行。每个 \nextgroupplot 按阅读顺序开始下一个单元格。

一行中的两个图表

\documentclass{article}
\usepackage{pgfplots}
\usepgfplotslibrary{groupplots}
\begin{document}
\begin{tikzpicture}
  \begin{groupplot}[
      group style={group size=2 by 1, horizontal sep=1.5cm},
      width=6cm, height=4.5cm, xlabel=$t$,
    ]
    \nextgroupplot[title={Position}, ylabel=$x$]
    \addplot[blue, thick, domain=0:4, samples=40] {x^2/2};
    \nextgroupplot[title={Velocity}, ylabel=$v$]
    \addplot[red, thick, domain=0:4] {x};
  \end{groupplot}
\end{tikzpicture}
\end{document}

groupplot 上的选项作用于每个单元格;\nextgroupplot 上的选项只作用于该单元格。

共享标签的 2 × 2 网格

在网格中,内侧的刻度标签和坐标轴标签会重复出现。xlabels at=edge bottom 和 ylabels at=edge left 只在外侧的图表上保留坐标轴标签,xticklabels at=edge bottom 对 x 刻度标签也起同样的作用。

标签只在外侧边缘的四个图表

\documentclass{article}
\usepackage{pgfplots}
\usepgfplotslibrary{groupplots}
\begin{document}
\begin{tikzpicture}
  \begin{groupplot}[
      group style={
        group size=2 by 2,
        horizontal sep=1.2cm, vertical sep=1cm,
        xlabels at=edge bottom, ylabels at=edge left,
        xticklabels at=edge bottom,
      },
      width=5.5cm, height=4cm,
      xlabel=$t$, ylabel=$y$, domain=0:6, samples=50,
    ]
    \nextgroupplot \addplot[blue] {sin(deg(x))};
    \nextgroupplot \addplot[red] {cos(deg(x))};
    \nextgroupplot \addplot[teal] {exp(-x/2)};
    \nextgroupplot \addplot[orange] {x/6};
  \end{groupplot}
\end{tikzpicture}
\end{document}

主图和残差面板

单元格可以有不同的高度和范围。一种常见的布局是数据图下方放一个小的残差面板,两者共享 x 轴。

数据和残差

\documentclass{article}
\usepackage{pgfplots}
\usepgfplotslibrary{groupplots}
\begin{document}
\begin{tikzpicture}
  \begin{groupplot}[
      group style={group size=1 by 2, vertical sep=0.4cm, xticklabels at=edge bottom},
      width=8cm, xmin=0, xmax=5,
    ]
    \nextgroupplot[height=5cm, ylabel={Signal}]
    \addplot+[only marks] coordinates {(1,2.1) (2,3.9) (3,6.2) (4,7.8)};
    \addplot[red, domain=0:5] {2*x};
    \nextgroupplot[height=3cm, xlabel=$x$, ylabel={Residual}, ymin=-0.5, ymax=0.5]
    \addplot+[only marks] coordinates {(1,0.1) (2,-0.1) (3,0.2) (4,-0.2)};
    \addplot[gray, dashed, domain=0:5] {0};
  \end{groupplot}
\end{tikzpicture}
\end{document}

group style 的键

键作用在 Scissor LaTeX 中
group size=C by R列数 × 行数支持
horizontal sep、vertical sep坐标轴边框之间的间距支持
xlabels at=edge bottom、ylabels at=edge left只在外侧图表上显示坐标轴标签(或 all)支持
xticklabels at=edge bottom、yticklabels at=edge left只在外侧图表上显示刻度标签(或 all)支持
group name=g用于引用单元格的名称,如 g c1r1可接受;不支持引用单元格

与 TeX 发行版的不同之处

这个库是 pgfplots 的一部分;请参阅 CTAN 上手册的“groupplots”一节:ctan.org/pkg/pgfplots。如果你刚接触 pgfplots,请从用 pgfplots 绘图开始。

另请参阅折线图、对数坐标轴和 pgfplots。

常见问题

如何在 LaTeX 中绘制子图?

使用 pgfplots 的 groupplots 库:\usepgfplotslibrary{groupplots},然后写 \begin{groupplot}[group style={group size=2 by 2}],每个图表一个 \nextgroupplot。

如何在组图中共享坐标轴标签?

在 group style 中加 xlabels at=edge bottom 和 ylabels at=edge left,并在 groupplot 上设置 xlabel 和 ylabel。

两个图表可以共用一个图例吗?

每个单元格有自己的图例。把图例放在一个单元格中,并用 legend style={at={(x,y)}, anchor=...} 设置它的位置。

本页的每个示例都在构建网站时由 Scissor LaTeX 排版。 最后更新:2026-11-07