0
0
Matplotlibdata~10 mins

Color mapping with colorbar in Matplotlib - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Color mapping with colorbar
Create data values
Choose colormap
Map data to colors
Plot data with colors
Add colorbar to show scale
Display plot
This flow shows how data values are linked to colors using a colormap, then plotted with a colorbar to explain the color scale.
Execution Sample
Matplotlib
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.colors import Normalize

values = np.array([1, 2, 3, 4, 5])
colors = plt.cm.viridis(values / 5)

plt.scatter(values, values, color=colors)
plt.colorbar(plt.cm.ScalarMappable(cmap='viridis', norm=Normalize(0, 5)))
plt.show()
This code maps numeric values to colors using the 'viridis' colormap, plots them, and adds a colorbar to show the color scale.
Execution Table
StepActionData/VariableResult/Output
1Create data arrayvalues[1 2 3 4 5]
2Normalize values for colormapvalues / 5[0.2 0.4 0.6 0.8 1.0]
3Map normalized values to colorsplt.cm.viridis(values / 5)Array of RGBA colors
4Plot scatter points with colorsplt.scatterScatter plot with colored points
5Create ScalarMappable for colorbarScalarMappable(cmap='viridis', norm=Normalize(0, 5))Colorbar object linked to data range
6Add colorbar to plotplt.colorbarColorbar displayed alongside plot
7Show plotplt.show()Plot window appears with points and colorbar
8End-Execution complete
💡 All steps executed; plot with color-mapped points and colorbar displayed.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
values-[1 2 3 4 5][1 2 3 4 5][1 2 3 4 5][1 2 3 4 5]
normalized_values--[0.2 0.4 0.6 0.8 1.0][0.2 0.4 0.6 0.8 1.0][0.2 0.4 0.6 0.8 1.0]
colors---Array of RGBA colorsArray of RGBA colors
Key Moments - 3 Insights
Why do we divide values by 5 before applying the colormap?
Dividing by 5 normalizes the data to the range 0 to 1, which is required because colormaps expect input values between 0 and 1. See execution_table step 2.
What does the ScalarMappable do for the colorbar?
ScalarMappable links the colormap and normalization to the colorbar, so it knows how to map data values to colors. This is shown in execution_table step 5.
Why do the colors in the scatter plot match the colorbar scale?
Because both use the same colormap and normalization, the colors of points correspond exactly to the colorbar's color scale. See steps 3 and 6.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table step 2, what is the normalized value for the original data value 3?
A0.6
B0.3
C3
D1.5
💡 Hint
Check the 'Data/Variable' column in step 2 showing values divided by 5.
At which step is the colorbar object created?
AStep 3
BStep 6
CStep 5
DStep 4
💡 Hint
Look at the 'Action' column describing ScalarMappable creation.
If we change the normalization range from (0,5) to (1,5), what happens to the color mapping?
AColors stay the same, no change
BColors shift to include lower values, changing the colorbar scale
CPlot will error due to invalid normalization
DColorbar disappears
💡 Hint
Normalization controls how data values map to colors; changing range affects colorbar and colors.
Concept Snapshot
Color mapping links data values to colors using a colormap.
Normalize data to 0-1 before mapping.
Plot data points with mapped colors.
Add a colorbar using ScalarMappable to show color scale.
Colorbar helps interpret colors in the plot.
Full Transcript
This visual execution shows how to map numeric data to colors using matplotlib's colormap. First, data values are created and normalized to a 0 to 1 range because colormaps expect inputs in this range. Then, these normalized values are converted to RGBA colors using a chosen colormap, here 'viridis'. The colored points are plotted using scatter. To explain the color meaning, a colorbar is added by creating a ScalarMappable object that links the colormap and normalization. Finally, the plot with colored points and the colorbar is displayed. Key moments include understanding normalization, the role of ScalarMappable, and how colors correspond between points and the colorbar.