Matplotlib colorbar background and label placement

Matplotlib colorbars can be customized with background styling and precise label placement. This involves creating contour plots and configuring the colorbar's appearance and tick labels.

Basic Colorbar with Custom Labels

First, let's create a simple colorbar with custom tick labels ?

import numpy as np
import matplotlib.pyplot as plt

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

# Create sample data
data = np.linspace(0, 10, num=16).reshape(4, 4)

# Create contour plot
cf = plt.contourf(data, levels=(0, 2.5, 5, 7.5, 10))

# Add colorbar with custom labels
cb = plt.colorbar(cf)
cb.set_ticklabels(['Low', 'Medium-Low', 'Medium', 'Medium-High', 'High'])

plt.title('Colorbar with Custom Labels')
plt.show()

Colorbar Background Customization

You can customize the colorbar background and label appearance using various properties ?

import numpy as np
import matplotlib.pyplot as plt

# Create sample data
data = np.random.rand(10, 10) * 100

# Create plot
fig, ax = plt.subplots(figsize=(8, 6))
im = ax.imshow(data, cmap='viridis')

# Create colorbar with background customization
cb = plt.colorbar(im, ax=ax)

# Customize colorbar appearance
cb.ax.tick_params(labelsize=12, colors='blue')
cb.set_label('Temperature (°C)', rotation=270, labelpad=20, fontsize=14, color='red')

# Set background color for colorbar
cb.ax.set_facecolor('lightgray')

# Customize tick labels
cb.set_ticks([0, 25, 50, 75, 100])
cb.set_ticklabels(['Cold', 'Cool', 'Warm', 'Hot', 'Very Hot'])

plt.title('Colorbar with Background Customization')
plt.show()

Advanced Label Placement

For more precise control over label placement and styling ?

import numpy as np
import matplotlib.pyplot as plt

# Generate sample data
x = np.linspace(0, 10, 100)
y = np.linspace(0, 10, 100)
X, Y = np.meshgrid(x, y)
Z = np.sin(X) * np.cos(Y) * 10

# Create contour plot
fig, ax = plt.subplots(figsize=(10, 6))
cs = ax.contourf(X, Y, Z, levels=20, cmap='coolwarm')

# Create colorbar with advanced customization
cb = plt.colorbar(cs, ax=ax, shrink=0.8, pad=0.1)

# Advanced label placement
cb.ax.yaxis.set_label_position('left')
cb.ax.yaxis.label.set_rotation(90)
cb.set_label('Amplitude', fontsize=16, weight='bold', color='darkblue')

# Custom tick formatting
ticks = np.linspace(Z.min(), Z.max(), 6)
cb.set_ticks(ticks)
cb.set_ticklabels([f'{tick:.1f}' for tick in ticks])

# Style the tick labels
cb.ax.tick_params(labelsize=10, colors='darkgreen', width=2)

# Add border around colorbar
for spine in cb.ax.spines.values():
    spine.set_edgecolor('black')
    spine.set_linewidth(2)

plt.title('Advanced Colorbar Label Placement', fontsize=18)
plt.tight_layout()
plt.show()

Key Customization Options

Property Method/Parameter Purpose
Label Position set_label_position() Move label to left/right
Background Color set_facecolor() Change colorbar background
Tick Colors tick_params(colors=...) Customize tick label colors
Label Padding labelpad parameter Adjust distance from colorbar

Conclusion

Matplotlib colorbar customization involves using colorbar() with various styling options. Use set_ticklabels() for custom labels, tick_params() for appearance, and set_label() for positioning and styling the main label.

Updated on: 2026-03-25T22:53:36+05:30

551 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements