For figures with colorbars, start with Matplotlib’s layout="constrained" and pass the relevant axes to fig.colorbar. Use GridSpec to define the figure’s row-and-column structure; use a layout engine to manage spacing. tight_layout remains an option, but Matplotlib describes constrained layout as the more modern built-in engine and documents it for colorbar-heavy arrangements.
Why a colorbar can change subplot sizes
A colorbar needs space inside the figure. When Matplotlib creates one, it may take that space from its parent axes. In a grid of plots, this can leave the axes with different sizes, making comparisons harder to read. Matplotlib’s colorbar placement guide documents this effect and shows how layout choice and axes association affect the result.
The key decision is which axes the colorbar belongs to. A colorbar for one plot can be associated with that axes; a shared colorbar should be associated with the intended group instead of an arbitrary single axes.
Use constrained layout for automatic colorbar accommodation
Create the figure with layout="constrained", then pass the axes associated with the colorbar to fig.colorbar. For a shared colorbar, pass the collection of axes it serves. Matplotlib’s constrained layout guide demonstrates colorbars associated with multiple axes, including selected portions of a grid.
#1 Best Overall
import matplotlib.pyplot as plt
import numpy as np
fig, axs = plt.subplots(2, 2, layout="constrained")
# Replace this example data with the arrays for your plots.
data = np.arange(100).reshape(10, 10)
for ax in axs.flat:
image = ax.imshow(data)
fig.colorbar(image, ax=axs)
plt.show()
Here, ax=axs tells Matplotlib that the colorbar serves the grid, so the layout engine can account for the group rather than treating one subplot as its sole parent. If the colorbar serves only a subset, pass only those axes. Inspect the rendered figure to confirm the axes remain suitable for comparison.
When to use tight_layout
tight_layout is Matplotlib’s earlier built-in layout approach; constrained layout is the more modern engine. They are alternative ways to manage spacing, not settings to stack casually. The layout engine API documents them as separate engines.
Rank #2
For a figure where the central challenge is making room for colorbars while keeping a subplot group coherent, prefer constrained layout. If you choose tight_layout, treat it as the layout approach for the figure and check the result with its actual labels, titles, and colorbars. Do not assume that enabling both engines will improve the fit.
Use GridSpec to define subplot structure
GridSpec describes how axes are arranged in logical rows and columns. It lets you set relative width and height ratios, create axes spanning multiple cells, and build nested sublayouts. It defines the structure; the layout engine adjusts spacing and fit within that structure.
Do these 3 things before closing this tab:
1Scan for outdated or missing drivers - takes under a minute2Clear out junk files and repair common Windows errors3Fix the driver behind crashes, sound loss and screen glitchesimport matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
import numpy as np
fig = plt.figure(layout="constrained")
gs = gridspec.GridSpec(2, 2, figure=fig, width_ratios=[2, 1])
ax_main = fig.add_subplot(gs[:, 0])
ax_top = fig.add_subplot(gs[0, 1])
ax_bottom = fig.add_subplot(gs[1, 1])
# Replace this example data with the arrays for your plots.
data = np.arange(100).reshape(10, 10)
image = ax_main.imshow(data)
ax_top.imshow(data)
ax_bottom.imshow(data)
fig.colorbar(image, ax=[ax_main])
plt.show()
This example gives the main plot a column that is twice the relative width of the other column, which is divided into two rows. The constrained layout engine manages spacing, while GridSpec supplies the arrangement. For more involved designs, Matplotlib’s current guides show nested GridSpec layouts and colorbars shared across selected axes.
Choose the combination that fits your figure
| Need | Use | What it does |
|---|---|---|
| Automatic room for a colorbar and a coherent subplot group | layout="constrained" with fig.colorbar(..., ax=...) |
The layout engine accounts for the colorbar and the axes it serves. |
| Explicit rows, columns, relative proportions, spanning, or nested regions | GridSpec, usually alongside a layout engine |
Defines the figure’s structural arrangement; it does not replace spacing management. |
| An alternative built-in approach to spacing | tight_layout |
Uses the earlier layout engine; assess its fit on the rendered figure. |
Troubleshoot uneven or cramped layouts
- Check the colorbar’s axes association. For a shared colorbar, pass all intended axes; for a colorbar serving part of a grid, pass that subset.
- Compare axes that should match. A colorbar can reduce its parent axes’ available space. If comparable plots end up at different sizes, reassess which axes the colorbar is attached to and try constrained layout with the correct group.
- Inspect the final rendering. Long labels, titles, and colorbars all compete for figure space; judge the actual output, not just the code or axes arrangement.
- Simplify a layout that collapses. Matplotlib’s constrained layout guide identifies insufficient available space and bugs as possible causes. Reduce the layout’s demands; if behavior still appears erroneous, provide a reproducible example when reporting it.
One colorbar-specific detail: use_gridspec=True is ignored by constrained layout. Matplotlib documents that option as intended to improve layout via tight_layout, so it is not a switch to add when using constrained layout.
Quick Recap
Best Value
Rank #4
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.




