Skip to content

Several plots in a grid with groupplots

Load the groupplots library, open a groupplot environment with group style={group size=2 by 1}, and start each plot with \nextgroupplot.

Two plots side by side

The groupplots library lays out several axes in a grid, with the same size and aligned frames. group size=2 by 1 means two columns and one row. Each \nextgroupplot starts the next cell, in reading order.

Two plots in one row

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

Options on groupplot apply to every cell; options on \nextgroupplot only to that one.

A 2 by 2 grid with shared labels

In a grid, the inner tick labels and axis labels repeat. xlabels at=edge bottom and ylabels at=edge left keep the axis labels on the outer plots only, and xticklabels at=edge bottom does the same for the x tick labels.

Four plots with labels on the outer edges

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

A main plot and a residual panel

Cells can have different heights and ranges. A common layout is a data plot with a small panel of residuals under it, sharing the x axis.

Data and residuals

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

KeyWhat it doesIn Scissor LaTeX
group size=C by Rcolumns by rowsWorks
horizontal sep, vertical sepspace between the axis framesWorks
xlabels at=edge bottom, ylabels at=edge leftaxis labels on the outer plots only (or all)Works
xticklabels at=edge bottom, yticklabels at=edge lefttick labels on the outer plots only (or all)Works
group name=ga name to refer to cells, as g c1r1Accepted; references to cells are not supported

What differs from a TeX installation

The library is part of pgfplots; see the "groupplots" section of the manual on CTAN: ctan.org/pkg/pgfplots. Start with plots with pgfplots if you are new to pgfplots.

See also Line plots, Logarithmic axes and pgfplots.

Questions and answers

How do I make subplots in LaTeX?

Use the pgfplots groupplots library: \usepgfplotslibrary{groupplots}, then \begin{groupplot}[group style={group size=2 by 2}] and one \nextgroupplot per plot.

How do I share the axis labels in a group plot?

Add xlabels at=edge bottom and ylabels at=edge left to group style, and set xlabel and ylabel on the groupplot.

Can two plots share one legend?

Each cell has its own legend. Put the legend in one cell and place it with legend style={at={(x,y)}, anchor=...}.

Every example on this page is typeset by Scissor LaTeX when the site is built. Last updated 2026-11-06.