0
0
Matplotlibdata~10 mins

Custom colormaps in Matplotlib - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Custom colormaps
Define colors or color list
Create colormap object
Use colormap in plot
Display plot with custom colors
You start by choosing colors, then create a colormap object, use it in a plot, and finally display the plot with your custom colors.
Execution Sample
Matplotlib
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.colors import LinearSegmentedColormap

colors = ['blue', 'white', 'red']
cm = LinearSegmentedColormap.from_list('my_cmap', colors)

x = np.linspace(0, 10, 100)
y = np.sin(x)
plt.scatter(x, y, c=y, cmap=cm)
plt.colorbar()
plt.show()
This code creates a custom colormap from blue to white to red and uses it to color points in a scatter plot based on their y-values.
Execution Table
StepActionInput/VariableOutput/Result
1Define colors listcolors = ['blue', 'white', 'red']colors list created
2Create colormapLinearSegmentedColormap.from_list('my_cmap', colors)cm = custom colormap object
3Generate datax = np.linspace(0,10,100), y = np.sin(x)x and y arrays created
4Plot scatterplt.scatter(x, y, c=y, cmap=cm)Scatter plot with colors from cm
5Add colorbarplt.colorbar()Colorbar shown with custom colors
6Show plotplt.show()Plot window opens with colored scatter
7EndNo more codeExecution stops
💡 All steps completed, plot displayed and execution ends
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4Final
colorsundefined['blue', 'white', 'red']['blue', 'white', 'red']['blue', 'white', 'red']['blue', 'white', 'red']['blue', 'white', 'red']
cmundefinedundefinedcustom colormap objectcustom colormap objectcustom colormap objectcustom colormap object
xundefinedundefinedundefinedarray of 100 floats from 0 to 10array of 100 floats from 0 to 10array of 100 floats from 0 to 10
yundefinedundefinedundefinedarray of 100 sin valuesarray of 100 sin valuesarray of 100 sin values
Key Moments - 3 Insights
Why do we use LinearSegmentedColormap.from_list instead of just a list of colors?
Because matplotlib needs a colormap object to map numeric values to colors smoothly. The list alone is just colors, but from_list creates a colormap that interpolates between them, as shown in execution_table step 2.
How does the scatter plot know which color to use for each point?
The scatter plot uses the 'c' argument with y-values and the custom colormap 'cm' to map each y-value to a color. This mapping happens in execution_table step 4.
What does plt.colorbar() do with a custom colormap?
It shows a color scale that matches the custom colormap, helping us understand which colors correspond to which data values, as seen in execution_table step 5.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of 'cm' after step 2?
AA list of colors
BA custom colormap object
CAn array of numbers
DUndefined
💡 Hint
Check the 'Output/Result' column for step 2 in execution_table.
At which step does the scatter plot get created with the custom colormap?
AStep 3
BStep 5
CStep 4
DStep 6
💡 Hint
Look for the action 'Plot scatter' in execution_table.
If you changed the colors list to ['green', 'yellow'], what would change in the variable_tracker?
AThe 'colors' variable after step 1 would be ['green', 'yellow']
BThe 'cm' variable would become undefined
CThe 'x' and 'y' variables would change
DNo variables would change
💡 Hint
Check how 'colors' is tracked in variable_tracker after step 1.
Concept Snapshot
Custom colormaps let you define your own color gradients.
Use matplotlib.colors.LinearSegmentedColormap.from_list(name, colors) to create one.
Pass this colormap to plotting functions with the cmap parameter.
Use plt.colorbar() to show the color scale.
This helps visualize data with colors you choose.
Full Transcript
This lesson shows how to create and use custom colormaps in matplotlib. First, you define a list of colors you want in your gradient. Then, you create a colormap object using LinearSegmentedColormap.from_list. Next, you generate some data and plot it using a scatter plot, applying your custom colormap to color the points based on their values. Adding plt.colorbar() displays the color scale. Finally, plt.show() opens the plot window. The execution table traces each step, showing how variables like colors, cm, x, and y change. Key moments clarify why a colormap object is needed, how colors map to data points, and the role of the colorbar. The quiz tests your understanding of these steps and variable changes. This visual trace helps beginners see exactly how custom colormaps work in practice.