0
0
Matplotlibdata~5 mins

Discrete colorbars in Matplotlib

Choose your learning style9 modes available
Introduction

Discrete colorbars help show clear steps of colors instead of smooth gradients. This makes it easier to see distinct groups or categories in data.

When you want to show categories or groups with different colors.
When your data has specific ranges that need clear color separation.
When you want to avoid confusion from smooth color changes in a legend.
When presenting data like survey results with fixed answer choices.
When visualizing data like temperature ranges or risk levels with clear steps.
Syntax
Matplotlib
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.colors as mcolors

# Define discrete colors and boundaries
cmap = mcolors.ListedColormap(['blue', 'green', 'yellow', 'red'])
bounds = [0, 10, 20, 30, 40]
norm = mcolors.BoundaryNorm(bounds, cmap.N)

# Create colorbar with discrete steps
fig, ax = plt.subplots(figsize=(6, 1))
fig.colorbar(plt.cm.ScalarMappable(norm=norm, cmap=cmap), cax=ax, orientation='horizontal')
plt.show()

Use ListedColormap to set specific colors for each step.

BoundaryNorm defines the edges of each color step.

Examples
This example creates a horizontal discrete colorbar with three colors: red, yellow, and green.
Matplotlib
import matplotlib.pyplot as plt
import matplotlib.colors as mcolors

cmap = mcolors.ListedColormap(['red', 'yellow', 'green'])
bounds = [0, 5, 10, 15]
norm = mcolors.BoundaryNorm(bounds, cmap.N)

fig, ax = plt.subplots(figsize=(5, 1))
fig.colorbar(plt.cm.ScalarMappable(norm=norm, cmap=cmap), cax=ax, orientation='horizontal')
plt.show()
This example uses four colors with boundaries at 0, 25, 50, 75, and 100 to show discrete steps.
Matplotlib
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.colors as mcolors

cmap = mcolors.ListedColormap(['navy', 'cyan', 'lime', 'orange'])
bounds = [0, 25, 50, 75, 100]
norm = mcolors.BoundaryNorm(bounds, cmap.N)

fig, ax = plt.subplots(figsize=(6, 1))
fig.colorbar(plt.cm.ScalarMappable(norm=norm, cmap=cmap), cax=ax, orientation='horizontal')
plt.show()
Sample Program

This program creates a horizontal discrete colorbar with four color steps representing value ranges 0-10, 10-20, 20-30, and 30-40.

Matplotlib
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.colors as mcolors

# Data values
values = np.array([5, 12, 18, 27, 33])

# Define discrete colormap and boundaries
colors = ['blue', 'green', 'yellow', 'red']
cmap = mcolors.ListedColormap(colors)
bounds = [0, 10, 20, 30, 40]
norm = mcolors.BoundaryNorm(bounds, cmap.N)

# Create a figure and axis
fig, ax = plt.subplots(figsize=(6, 1))

# Create a ScalarMappable for the colorbar
sm = plt.cm.ScalarMappable(cmap=cmap, norm=norm)
sm.set_array([])  # Needed for colorbar

# Add discrete colorbar
cbar = fig.colorbar(sm, cax=ax, orientation='horizontal')
cbar.set_label('Value ranges')

plt.show()
OutputSuccess
Important Notes

Make sure the number of colors matches the number of intervals defined by bounds minus one.

Use set_array([]) on ScalarMappable to avoid warnings when creating colorbars without data.

Summary

Discrete colorbars show clear color steps for distinct data ranges.

Use ListedColormap and BoundaryNorm to create discrete colorbars.

They help make legends easier to read when data has categories or fixed ranges.