How to show multiple colorbars in Matplotlib?

Creating multiple colorbars in Matplotlib allows you to visualize different datasets with their own color scales. This is particularly useful when displaying multiple subplots with different data ranges or when you want to compare datasets side by side.

Steps to Create Multiple Colorbars

To show multiple colorbars in matplotlib, we can take the following steps −

  • Set the figure size and adjust the padding between and around the subplots.
  • Create a figure and a set of subplots.
  • Initialize a variable N for the number of sample data.
  • Create random data1 using numpy.
  • Display data as an image, i.e., on a 2D regular raster, with data1.
  • Add a colorbar to a plot.
  • Repeat steps 4, 5, and 6, with different datasets and axes.
  • To display the figure, use show() method.

Method 1: Using Separate Axes for Colorbars

The most common approach is to create separate axes for each colorbar ?

import matplotlib.pyplot as plt
import numpy as np

plt.rcParams["figure.figsize"] = [10, 6]
plt.rcParams["figure.autolayout"] = True

fig, (ax1, ax2) = plt.subplots(nrows=1, ncols=2)
N = 10

# First subplot with 'hot' colormap
data1 = np.random.rand(N, N)
im1 = ax1.imshow(data1, cmap='hot')
ax1.set_title('Hot Colormap')
plt.colorbar(im1, ax=ax1)

# Second subplot with 'plasma' colormap
data2 = np.random.rand(N, N) * 100  # Different scale
im2 = ax2.imshow(data2, cmap='plasma')
ax2.set_title('Plasma Colormap')
plt.colorbar(im2, ax=ax2)

plt.tight_layout()
plt.show()

Method 2: Vertical Layout with Multiple Colorbars

You can also arrange subplots vertically, each with its own colorbar ?

import matplotlib.pyplot as plt
import numpy as np

fig, (ax1, ax2) = plt.subplots(nrows=2, ncols=1, figsize=(8, 10))
N = 15

# First subplot
data1 = np.random.rand(N, N)
im1 = ax1.imshow(data1, cmap='viridis')
ax1.set_title('Viridis Colormap')
plt.colorbar(im1, ax=ax1, orientation='horizontal', pad=0.1)

# Second subplot
data2 = np.random.rand(N, N) * 50
im2 = ax2.imshow(data2, cmap='coolwarm')
ax2.set_title('Coolwarm Colormap')
plt.colorbar(im2, ax=ax2, orientation='horizontal', pad=0.1)

plt.tight_layout()
plt.show()

Method 3: Custom Positioning for Colorbars

For more control, you can manually position colorbars using make_axes_locatable ?

import matplotlib.pyplot as plt
import numpy as np
from mpl_toolkits.axes_grid1 import make_axes_locatable

fig, (ax1, ax2) = plt.subplots(nrows=1, ncols=2, figsize=(12, 5))
N = 12

# First subplot
data1 = np.random.rand(N, N)
im1 = ax1.imshow(data1, cmap='inferno')
ax1.set_title('Inferno Colormap')

# Create divider for first colorbar
divider1 = make_axes_locatable(ax1)
cax1 = divider1.append_axes("right", size="5%", pad=0.1)
plt.colorbar(im1, cax=cax1)

# Second subplot
data2 = np.random.rand(N, N) * 10
im2 = ax2.imshow(data2, cmap='spring')
ax2.set_title('Spring Colormap')

# Create divider for second colorbar
divider2 = make_axes_locatable(ax2)
cax2 = divider2.append_axes("right", size="5%", pad=0.1)
plt.colorbar(im2, cax=cax2)

plt.tight_layout()
plt.show()

Key Parameters

Parameter Description Options
ax Associates colorbar with specific axes ax1, ax2, etc.
orientation Colorbar direction 'vertical', 'horizontal'
pad Space between plot and colorbar 0.1, 0.2, etc.
size Width/height of colorbar "5%", "10%", etc.

Conclusion

Multiple colorbars in Matplotlib help visualize datasets with different scales effectively. Use the ax parameter to associate each colorbar with its corresponding subplot, and consider using make_axes_locatable for precise positioning control.

Updated on: 2026-03-25T20:14:57+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements