0
0
Matplotlibdata~10 mins

Colormaps (sequential, diverging, qualitative) in Matplotlib - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Colormaps (sequential, diverging, qualitative)
Choose Data Type
Select Colormap Type
Sequential
Apply Colors to Data
Visualize with Matplotlib
Start by choosing the data type, then pick a colormap type (sequential, diverging, or qualitative), apply colors accordingly, and finally visualize the data.
Execution Sample
Matplotlib
import matplotlib.pyplot as plt
import numpy as np

# Create data
x = np.linspace(0, 10, 100)
y = np.sin(x)

# Plot with sequential colormap
plt.scatter(x, y, c=y, cmap='viridis')
plt.colorbar()
plt.show()
This code plots points colored by their y-value using a sequential colormap 'viridis'.
Execution Table
StepActionData ValuesColormap TypeColor AppliedOutput
1Generate x and y datax: 0 to 10, y: sin(x)N/AN/AData arrays created
2Choose colormapN/ASequential ('viridis')Colors from dark purple to yellowColormap selected
3Map y values to colorsy values between -1 and 1SequentialColors mapped from low to high yColors assigned to points
4Plot scatter with colorsx and y arraysSequentialColors shown on pointsScatter plot displayed
5Add colorbarN/ASequentialColor scale shownColorbar displayed
6Show plotN/ASequentialColors visiblePlot window opens
7EndN/AN/AN/AExecution complete
💡 Plot displayed and program ends
Variable Tracker
VariableStartAfter Step 1After Step 3Final
xundefinedarray from 0 to 10unchangedunchanged
yundefinedsin(x) valuesunchangedunchanged
cmapundefinedundefined'viridis''viridis'
colorsundefinedundefinedmapped colors from yunchanged
Key Moments - 3 Insights
Why do we use different colormap types for different data?
Sequential colormaps are for ordered data from low to high (see Step 2 and 3 in execution_table). Diverging colormaps highlight deviation around a midpoint, and qualitative colormaps are for categorical data without order.
What happens if you use a qualitative colormap for continuous data?
Colors will not represent order or magnitude well, causing confusion. The execution_table shows sequential mapping for continuous y values, which qualitative colormaps do not support.
How does the colorbar relate to the colormap?
The colorbar (Step 5) visually shows how data values map to colors in the colormap, helping interpret the plot colors.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at Step 3. What type of data is being mapped to colors?
ACategorical data
BContinuous numerical data
CBoolean data
DText data
💡 Hint
Check the 'Data Values' column at Step 3 showing y values between -1 and 1.
At which step is the colormap chosen in the execution_table?
AStep 2
BStep 1
CStep 4
DStep 5
💡 Hint
Look at the 'Action' column for when the colormap is selected.
If you replaced 'viridis' with a qualitative colormap, what would change in the 'Color Applied' column at Step 3?
AColors would represent a smooth gradient
BColors would be grayscale
CColors would be distinct blocks without order
DColors would be random
💡 Hint
Refer to the 'Colormap Type' and 'Color Applied' columns at Step 3.
Concept Snapshot
Colormaps assign colors to data values.
Sequential: for ordered data from low to high.
Diverging: for data with a meaningful midpoint.
Qualitative: for categorical data without order.
Use matplotlib's cmap parameter to apply.
Add colorbar to show color scale.
Full Transcript
This visual execution shows how to use colormaps in matplotlib. First, data arrays x and y are created. Then, a sequential colormap 'viridis' is chosen to map continuous y values to colors. The colors are applied to points in a scatter plot. A colorbar is added to explain the color scale. The plot is displayed, showing how colors represent data values. Key points include choosing the right colormap type for your data and understanding how colors map to values.