Layouts and multiple charts
Matplotlib Basics
2 min read
Published Oct 5 2025
Guide Sections
Guide Comments
Matplotlib gives you several flexible ways to arrange multiple charts (axes) in a single figure.
Key Concepts:
Figure= The overall canvas or page where all plots liveAxes= A single plot area (with x/y axes, labels, etc.)Subplot= A single Axes positioned within a grid inside the FigureLayout= How those Axes are arranged (rows × columns)
A single figure can contain multiple axes in a grid layout or even precisely positioned with gridspec.
The Three Main Approaches:
Method | When to use | Description |
plt.subplot() | Simple, small grids | Quick creation (old style) |
plt.subplots() | Modern and flexible | Preferred for most uses |
GridSpec | Complex / unequal layouts | Fine control of positioning |
Quick & simple — plt.subplot()
plt.subplot(nrows, ncols, index) Creates one subplot in a grid:
Quick but limited — you have to call it before each plot.

Modern & preferred — plt.subplots()
This returns both a figure and axes objects, letting you manage everything cleanly:
axes is a NumPy array — index as [row, column].

1D layouts — row or column only
If you only have one row or column, axes becomes 1D:
Access as axes[0], axes[1], etc.

Shared axes
You can make subplots share an axis scale:
sharex=True or sharey=True

Adjusting spacing between Subplots
or simply:
tight_layout() automatically fits titles and labels neatly.
Unequal layouts — GridSpec
Use matplotlib.gridspec for non-uniform layouts:
Very flexible — choose cell ranges like [row_start:row_end, col_start:col_end].

Mixed plot types
You can mix bar, line, scatter, pie, etc., in one figure:
Each Axes object acts independently.

Compact multi-chart using plt.subplot_mosaic
A more readable layout system:
- To tell Matplotlib that
"top"spans both columns, repeat the key name in the list. - You can reference axes by name, not index — great for complex layouts.

Dashboard style layout
constrained_layout=True automatically optimizes spacing between subplots.















