Crashes, No Sound, or Screen Glitches?
Random freezes, missing sound and display glitches usually trace back to one bad driver. Find and replace yours safely.Free scan · under a minutePC Slower Than It Used to Be?
A free scan shows the junk files, broken settings and background clutter dragging Windows down - then fixes them in one click.Free scan · Windows 10 & 11Call fig.colorbar once for each subplot, passing it that subplot’s mappable (the object returned by its plotting function) and the subplot’s axes. For a standard grid, layout="constrained" lets Matplotlib make room for the individual colorbars.
Add a separate colorbar to every subplot
Plot each panel, keep the returned mappable, then associate it with its axes in fig.colorbar. Here is a complete example with four images:
import matplotlib.pyplot as plt
import numpy as np
fig, axs = plt.subplots(2, 2, layout="constrained")
data = np.arange(100).reshape(10, 10)
for i, ax in enumerate(axs.flat):
image = ax.imshow(data * (i + 1), cmap="viridis")
fig.colorbar(image, ax=ax, label=f"Panel {i + 1}")
plt.show()
imshow returns an image mappable, which supplies the data-to-color mapping used by the colorbar. The same pattern applies to supported plot artists such as pcolormesh and contour plots: pass the object returned by that plotting call. The ax argument identifies the subplot associated with the colorbar and tells the figure where to make room. See the Figure.colorbar API and pyplot.colorbar API.
Make room for the colorbars
For an ordinary subplot grid, use layout="constrained" when creating the figure. Matplotlib’s constrained layout guide describes automatic space allocation for colorbars, including colorbars attached to individual axes or an axes collection.
The Tool Desk
Outbyte PC Repair FREERepair Windows errors before they cause bigger problemsFix Now →Outbyte Driver Updater FREEFix the driver behind crashes, sound loss and screen glitchesFind Drivers →#1 Best Overall
If you need more control over placement, create a dedicated colorbar axes and pass it as cax instead of relying on ax to determine where space is taken from. When cax is supplied, it determines the colorbar’s size, so shrink and aspect are ignored. The AxesDivider colorbar example recommends passing the main axes through ax for basic placement rather than manually creating a locatable axes.
Use ImageGrid for a per-axes colorbar grid
If you are building the figure with mpl_toolkits.axes_grid1.ImageGrid, its cbar_mode="each" option creates a colorbar axes for each grid axes. Pair each image axes with the matching entry in cbar_axes:
Rank #2
from mpl_toolkits.axes_grid1 import ImageGrid
fig = plt.figure(layout="constrained")
grid = ImageGrid(
fig, 111,
nrows_ncols=(2, 2),
cbar_mode="each",
cbar_location="right",
cbar_size="5%",
cbar_pad="2%",
)
for i, (ax, cax) in enumerate(zip(grid, grid.cbar_axes)):
image = ax.imshow(data * (i + 1), cmap="viridis")
fig.colorbar(image, cax=cax)
See Matplotlib’s ImageGrid example and ImageGrid API for the grid and colorbar-axis options. For a normal plt.subplots layout, repeated fig.colorbar(..., ax=ax) calls are usually simpler.
Decide whether each subplot needs its own scale
Separate colorbars make sense when panels use independent mappings or you want each panel’s scale presented separately. If the values are intended to be compared directly, give the plots a common normalization and use one colorbar for the axes collection instead. Matplotlib’s multiple-image example demonstrates shared normalization with a single colorbar. A shared bar also uses less figure space.
Do these 3 things before closing this tab:
1Fix the driver behind crashes, sound loss and screen glitches2Clear out junk files and repair common Windows errors3Scan for outdated or missing drivers - takes under a minuteQuick Recap
Best Value
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.




