How to change the font properties of a Matplotlib colorbar label?

To change the font properties of a Matplotlib colorbar label, you can customize the label using various font parameters like weight, size, and family. Here's how to modify colorbar label properties effectively.

Steps to Change Colorbar Label Font Properties

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

  • Create x, y and z data points using numpy.

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

  • Create a colorbar for a ScalarMappable instance, mappable.

  • Using colorbar axes, set the font properties such that the label is bold.

  • To display the figure, use show() method.

Basic Example with Bold Font

Here's a complete example showing how to make the colorbar label bold ?

import numpy as np
import matplotlib.pyplot as plt

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

x, y = np.mgrid[-1:1:100j, -1:1:100j]
z = (x + y) * np.exp(-5.0 * (x ** 2 + y ** 2))

plt.imshow(z, extent=[-1, 1, -1, 1])

cb = plt.colorbar()
cb.set_label(label='My Color Bar Label', weight='bold')

plt.show()

Advanced Font Customization

You can customize multiple font properties including size, family, and style ?

import numpy as np
import matplotlib.pyplot as plt

# Create sample data
x, y = np.mgrid[-2:2:50j, -2:2:50j]
z = np.sin(x) * np.cos(y)

fig, ax = plt.subplots(figsize=(8, 6))
im = ax.imshow(z, extent=[-2, 2, -2, 2], cmap='viridis')

# Create colorbar with custom font properties
cb = plt.colorbar(im)
cb.set_label(label='Amplitude', 
             weight='bold', 
             size=14, 
             family='serif',
             style='italic')

plt.title('Colorbar with Custom Font Properties')
plt.show()

Multiple Font Properties

This example demonstrates various font customization options available ?

import numpy as np
import matplotlib.pyplot as plt

# Generate data
data = np.random.random((10, 10))

fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(12, 4))

# First subplot - default font
im1 = ax1.imshow(data, cmap='plasma')
cb1 = plt.colorbar(im1, ax=ax1)
cb1.set_label('Default Font')
ax1.set_title('Default Colorbar')

# Second subplot - customized font
im2 = ax2.imshow(data, cmap='plasma')
cb2 = plt.colorbar(im2, ax=ax2)
cb2.set_label('Custom Font Label', 
              weight='bold',
              size=16,
              family='monospace',
              color='darkblue')
ax2.set_title('Customized Colorbar')

plt.tight_layout()
plt.show()

Font Properties Summary

Property Options Description
weight 'normal', 'bold', 'light' Font weight/thickness
size Number or 'small', 'large' Font size
family 'serif', 'sans-serif', 'monospace' Font family
style 'normal', 'italic', 'oblique' Font style
color Color name or hex code Label color

Conclusion

Use the set_label() method on the colorbar object to customize font properties. Combine multiple parameters like weight='bold', size=14, and family='serif' to achieve the desired appearance for your colorbar labels.

Updated on: 2026-03-25T21:59:37+05:30

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements