跳到主要內容

用 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