How to give Matplolib imshow plot colorbars a label?

To add a label to a matplotlib imshow() plot colorbar, you can use the set_label() method on the colorbar object. This helps viewers understand what the color scale represents in your visualization.

Steps to Add Colorbar Labels

Here's the process for adding colorbar labels:

  • Set the figure size and adjust the padding between and around the subplots.

  • Create sample data using NumPy.

  • Use imshow() method to display the data as an image on a 2D regular raster.

  • Create a colorbar for the image using colorbar().

  • Set colorbar label using set_label() method.

  • Display the figure using show() method.

Basic Example

Here's how to add a simple label to your colorbar −

import numpy as np
import matplotlib.pyplot as plt

# Set figure size
plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True

# Create sample data
data = np.random.rand(5, 5)

# Display data as image
im = plt.imshow(data, cmap="copper")

# Create colorbar and add label
cbar = plt.colorbar(im)
cbar.set_label("Random Values")

plt.show()
Displays a heatmap with a colorbar labeled "Random Values"

Customizing Colorbar Labels

You can customize the label appearance with additional parameters −

import numpy as np
import matplotlib.pyplot as plt

# Create temperature data
temperature = np.random.uniform(20, 40, (8, 8))

# Create the plot
fig, ax = plt.subplots(figsize=(8, 6))
im = ax.imshow(temperature, cmap="coolwarm")

# Add customized colorbar label
cbar = plt.colorbar(im, ax=ax)
cbar.set_label("Temperature (°C)", rotation=270, labelpad=20, fontsize=12)

# Add title and show
ax.set_title("Temperature Distribution")
plt.show()
Displays a temperature heatmap with a rotated colorbar label "Temperature (°C)"

Multiple Subplots with Colorbars

When working with subplots, you can add individual colorbar labels −

import numpy as np
import matplotlib.pyplot as plt

# Create data
data1 = np.random.rand(6, 6)
data2 = np.random.rand(6, 6) * 100

# Create subplots
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(12, 5))

# First subplot
im1 = ax1.imshow(data1, cmap="viridis")
cbar1 = plt.colorbar(im1, ax=ax1)
cbar1.set_label("Probability")
ax1.set_title("Dataset 1")

# Second subplot
im2 = ax2.imshow(data2, cmap="plasma")
cbar2 = plt.colorbar(im2, ax=ax2)
cbar2.set_label("Count")
ax2.set_title("Dataset 2")

plt.tight_layout()
plt.show()
Shows two side-by-side heatmaps, each with its own labeled colorbar

Parameters for set_label()

Parameter Description Example Value
rotation Angle of label rotation 270 (vertical)
labelpad Distance from colorbar 15
fontsize Size of label text 12
fontweight Weight of label text 'bold'

Conclusion

Use cbar.set_label() to add descriptive labels to your colorbar. Customize with parameters like rotation, labelpad, and fontsize for better readability. This makes your data visualizations more informative and professional.

Updated on: 2026-03-26T00:27:54+05:30

5K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements