Matplotlib different size subplots
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Matplotlib makes equal-sized subplot grids easy with plt.subplots(), but real dashboards often need one chart to be larger than the others. The usual solution is to move from the simple grid helper to GridSpec or the newer subplot layout APIs that let axes span multiple rows or columns. Once you think in terms of grid cells instead of subplot indices, uneven layouts become straightforward.
Use GridSpec for Uneven Layouts
GridSpec lets you define a grid and then decide how many rows and columns each subplot should occupy.
Here the main plot spans both rows and the first two columns, while the two smaller plots stack on the right.
Control Relative Sizes With Ratios
Sometimes you do not need spanning, only different relative widths or heights. width_ratios and height_ratios control that.
This is useful when every subplot should still occupy its own cell, but some columns or rows should be visually larger.
subplot_mosaic for Readable Named Layouts
For dashboards, subplot_mosaic can be more readable than integer slices because you describe the layout with names.
This API is especially nice when you want the code to mirror the visual layout conceptually.
Spacing Still Matters
Uneven subplot sizes are only half the problem. Labels, titles, and colorbars can still overlap if spacing is not handled well.
Two common tools are:
- '
fig.tight_layout()' - '
constrained_layout=True'
If the figure has a complicated layout, prefer one consistent spacing strategy instead of stacking several manual adjustments on top of each other.
Start With the Layout, Then Plot
A practical habit is to design the subplot geometry before writing the plotting details. Decide which chart should dominate, which panels are secondary, and whether ratios or spanning communicate that priority better.
Doing that first usually leads to cleaner code than drawing several equal subplots and then retrofitting the layout afterward.
A Good Mental Model
The simplest way to design different-sized subplots is:
- decide on an invisible grid
- decide which axes span multiple cells
- adjust width and height ratios if needed
- clean up spacing last
That sequence is easier than starting from subplot indices and trying to patch the layout afterward.
Common Pitfalls
- Using
plt.subplots()and expecting different-sized axes without a custom grid system. - Forgetting that
GridSpecindexing is zero-based and slice-based. - Mixing
tight_layout()and heavy manual spacing adjustments until the figure becomes harder to control. - Creating a visually complex layout before deciding the underlying grid structure.
- Ignoring
width_ratiosandheight_ratioswhen simple proportional sizing would solve the problem.
Summary
- Use
GridSpecor related layout APIs when subplots need different sizes. - Let larger axes span multiple grid cells instead of fighting
plt.subplots(). - Use width and height ratios for proportional sizing.
- Consider
subplot_mosaicwhen named layouts are easier to read. - Handle spacing explicitly so the final figure stays readable.

