October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsPC HealthRecommendedCrashes, freezes, slowdowns? Check your PC nowSpot repairable issues before they interrupt work.Check PCOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk2 min

How to Add a Colorbar to Each Subplot in Matplotlib

Use each plot’s returned mappable with fig.colorbar(..., ax=ax) to add an individual colorbar to every Matplotlib subplot.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Call 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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

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:

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.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

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.

Leave a Reply

Your email address will not be published. Required fields are marked *

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Outdated Drivers Are Slowing You DownFree scan - exact matches
Windows Errors? Fix Them Before They SpreadFree repair scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.