0
0
Matplotlibdata~10 mins

Discrete colorbars in Matplotlib - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Discrete colorbars
Define color levels
Create colormap with discrete colors
Normalize data to levels
Plot data with colormap
Add colorbar with discrete ticks
Display plot
This flow shows how to create a colorbar with distinct color segments by defining levels, mapping colors discretely, and displaying the colorbar.
Execution Sample
Matplotlib
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.colors import BoundaryNorm

levels = [0, 1, 2, 3, 4]
cmap = plt.get_cmap('viridis', len(levels)-1)
norm = BoundaryNorm(levels, cmap.N)

fig, ax = plt.subplots()
img = ax.imshow(np.array([[0.5,1.5],[2.5,3.5]]), cmap=cmap, norm=norm)
fig.colorbar(img, boundaries=levels, ticks=levels)
plt.show()
This code creates a 2x2 image with values mapped to discrete color levels and shows a colorbar with distinct color segments.
Execution Table
StepActionInput/VariableResult/Output
1Define levelslevels = [0,1,2,3,4]Levels set for color boundaries
2Create colormapcmap = viridis with 4 colorsColormap with 4 discrete colors created
3Create normalizationnorm = BoundaryNorm(levels, cmap.N)Normalization maps data to discrete colors
4Prepare datadata = [[0.5,1.5],[2.5,3.5]]Data ready for plotting
5Plot imageimshow with cmap and normImage colored with discrete colors
6Add colorbarcolorbar with boundaries=levels, ticks=levelsColorbar shows 4 discrete color segments
7Display plotplt.show()Plot with discrete colorbar displayed
💡 Plot displayed with discrete colorbar matching defined levels
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4After Step 5After Step 6Final
levelsundefined[0,1,2,3,4][0,1,2,3,4][0,1,2,3,4][0,1,2,3,4][0,1,2,3,4][0,1,2,3,4][0,1,2,3,4]
cmapundefinedundefinedviridis with 4 colorsviridis with 4 colorsviridis with 4 colorsviridis with 4 colorsviridis with 4 colorsviridis with 4 colors
normundefinedundefinedundefinedBoundaryNorm objectBoundaryNorm objectBoundaryNorm objectBoundaryNorm objectBoundaryNorm object
dataundefinedundefinedundefinedundefined[[0.5,1.5],[2.5,3.5]][[0.5,1.5],[2.5,3.5]][[0.5,1.5],[2.5,3.5]][[0.5,1.5],[2.5,3.5]]
imgundefinedundefinedundefinedundefinedundefinedAxesImage objectAxesImage objectAxesImage object
Key Moments - 3 Insights
Why do we use BoundaryNorm instead of a simple normalization?
BoundaryNorm maps data values to discrete color intervals defined by levels, ensuring each color segment corresponds exactly to a range, as shown in step 3 of the execution_table.
Why do the colorbar ticks match the levels list?
The colorbar uses the boundaries and ticks parameters set to levels, so each tick aligns with a color segment boundary, as seen in step 6.
What happens if data values fall between levels?
BoundaryNorm assigns colors based on which interval the value falls into, so values like 0.5 map to the first color segment between 0 and 1, demonstrated in step 5.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table step 3. What does BoundaryNorm do with the levels?
ACreates a continuous gradient colormap
BSets the colorbar labels only
CMaps continuous data to discrete color intervals
DNormalizes data between 0 and 1
💡 Hint
Refer to step 3 where norm is created with BoundaryNorm using levels.
At which step is the image data assigned colors based on discrete levels?
AStep 4
BStep 5
CStep 2
DStep 6
💡 Hint
Step 5 shows plotting with imshow using cmap and norm.
If we add a new level 5 to levels, how does the colorbar change?
AIt shows 5 discrete color segments instead of 4
BIt remains with 4 color segments
CIt becomes a continuous gradient
DIt removes all ticks
💡 Hint
Check variable_tracker for levels and cmap size relation.
Concept Snapshot
Discrete colorbars in matplotlib:
- Define levels as boundaries for colors
- Use BoundaryNorm to map data to these levels
- Create colormap with number of colors = len(levels)-1
- Plot data with cmap and norm
- Add colorbar with boundaries and ticks matching levels
- Result: colorbar shows distinct color segments matching data ranges
Full Transcript
This example shows how to create a discrete colorbar in matplotlib. First, we define the levels that separate color segments. Then, we create a colormap with a fixed number of colors matching the intervals between levels. BoundaryNorm is used to map data values to these discrete color intervals. We plot the data using imshow with the colormap and normalization. Finally, we add a colorbar with boundaries and ticks set to the levels, so the colorbar shows distinct color blocks. This helps visualize data categories clearly instead of a smooth gradient.