How to build colorbars without attached plot in matplotlib?

A colorbar in matplotlib is typically attached to a plot to show the color mapping. However, you can create standalone colorbars without any attached plot using ColorbarBase. This is useful for legends or reference scales.

Creating a Basic Standalone Colorbar

Use ColorbarBase to create a colorbar without an associated plot ?

import matplotlib.pyplot as plt
import matplotlib as mpl

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

# Create figure and subplot
fig, ax = plt.subplots()

# Adjust layout to make room for colorbar
fig.subplots_adjust(bottom=0.5)

# Create normalization (data range)
norm = mpl.colors.Normalize(vmin=5, vmax=10)

# Create standalone colorbar
cb = mpl.colorbar.ColorbarBase(ax, cmap=mpl.cm.cool, norm=norm, orientation='horizontal')

# Add label
cb.set_label('Temperature (°C)')

plt.show()

Vertical Colorbar Example

Create a vertical standalone colorbar with custom positioning ?

import matplotlib.pyplot as plt
import matplotlib as mpl

# Create figure
fig = plt.figure(figsize=(6, 8))

# Create axis for colorbar
ax = fig.add_axes([0.3, 0.1, 0.4, 0.8])  # [left, bottom, width, height]

# Define normalization and colormap
norm = mpl.colors.Normalize(vmin=0, vmax=100)
cmap = mpl.cm.viridis

# Create vertical colorbar
cb = mpl.colorbar.ColorbarBase(ax, cmap=cmap, norm=norm, orientation='vertical')

# Customize colorbar
cb.set_label('Intensity (%)', rotation=270, labelpad=15)
cb.set_ticks([0, 25, 50, 75, 100])

plt.show()

Multiple Colorbars

Create multiple standalone colorbars in the same figure ?

import matplotlib.pyplot as plt
import matplotlib as mpl
import numpy as np

fig = plt.figure(figsize=(10, 3))

# First colorbar
ax1 = fig.add_subplot(131)
norm1 = mpl.colors.Normalize(vmin=-1, vmax=1)
cb1 = mpl.colorbar.ColorbarBase(ax1, cmap=mpl.cm.RdBu, norm=norm1)
cb1.set_label('Correlation')

# Second colorbar
ax2 = fig.add_subplot(132)
norm2 = mpl.colors.Normalize(vmin=0, vmax=10)
cb2 = mpl.colorbar.ColorbarBase(ax2, cmap=mpl.cm.plasma, norm=norm2)
cb2.set_label('Energy (eV)')

# Third colorbar with discrete colors
ax3 = fig.add_subplot(133)
colors = ['red', 'orange', 'yellow', 'green', 'blue']
cmap3 = mpl.colors.ListedColormap(colors)
norm3 = mpl.colors.BoundaryNorm(range(len(colors)+1), len(colors))
cb3 = mpl.colorbar.ColorbarBase(ax3, cmap=cmap3, norm=norm3)
cb3.set_label('Categories')

plt.tight_layout()
plt.show()

Key Parameters

Parameter Description Example Values
cmap Colormap to use 'viridis', 'plasma', 'cool'
norm Data normalization Normalize(vmin=0, vmax=1)
orientation Colorbar direction 'horizontal', 'vertical'

Conclusion

Use ColorbarBase to create standalone colorbars without plots. This is perfect for creating legends, reference scales, or when you need colorbars independent of data visualization.

Updated on: 2026-03-26T14:58:39+05:30

279 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements