groupplots で複数のグラフを格子状に並べる
groupplots ライブラリを読み込み、group style={group size=2 by 1} を指定して groupplot 環境を開き、各グラフを \nextgroupplot で始めます。
横に並べた 2 つのグラフ
groupplots ライブラリは、複数の軸を同じ大きさで枠をそろえて格子状に配置します。group size=2 by 1 は 2 列 1 行を意味します。各 \nextgroupplot は、読む順に次のセルを始めます。
1 行に並べた 2 つのグラフ
\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 の目盛りラベルについて同じことをします。
外側の辺にだけラベルを付けた 4 つのグラフ
\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 によるグラフから始めましょう。
よくある質問
LaTeX でサブプロットを作るにはどうすればよいですか?
pgfplots の groupplots ライブラリを使います。\usepgfplotslibrary{groupplots} と書き、\begin{groupplot}[group style={group size=2 by 2}] を開いて、グラフごとに \nextgroupplot を 1 つずつ書きます。
グループプロットで軸ラベルを共有するにはどうすればよいですか?
group style に xlabels at=edge bottom と ylabels at=edge left を追加し、groupplot に xlabel と ylabel を指定します。
2 つのグラフで 1 つの凡例を共有できますか?
凡例はセルごとに付きます。凡例を 1 つのセルに置き、legend style={at={(x,y)}, anchor=...} で位置を決めてください。
このページの例はすべて、サイトのビルド時に Scissor LaTeX で組版されています。 最終更新:2026-11-07