Bird
Raised Fist0
Matplotlibdata~10 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. Which type of colormap is best to use when your data has a natural order from low to high values?
easy
A. Sequential colormap
B. Diverging colormap
C. Qualitative colormap
D. Random colormap

Solution

  1. Step 1: Understand data order

    Sequential colormaps are designed for data that goes from low to high in a smooth gradient.
  2. Step 2: Match colormap type to data

    Diverging is for data with a middle point, qualitative is for categories, so sequential fits ordered data best.
  3. Final Answer:

    Sequential colormap -> Option A
  4. Quick Check:

    Ordered data = Sequential colormap [OK]
Hint: Ordered data? Pick sequential colormap [OK]
Common Mistakes:
  • Choosing diverging for ordered data
  • Using qualitative for numeric ranges
  • Thinking random colormap exists
2. Which of the following is the correct way to apply the 'coolwarm' diverging colormap to a matplotlib scatter plot?
easy
A. plt.scatter(x, y, c=z, cmap='coolwarm')
B. plt.scatter(x, y, color='coolwarm')
C. plt.scatter(x, y, cmap='coolwarm')
D. plt.scatter(x, y, c=z, color_map='coolwarm')

Solution

  1. Step 1: Check scatter plot parameters

    The parameter to set colors by data is c=, and colormap is set by cmap=.
  2. Step 2: Identify correct syntax

    plt.scatter(x, y, c=z, cmap='coolwarm') uses both c=z and cmap='coolwarm', which is correct syntax.
  3. Final Answer:

    plt.scatter(x, y, c=z, cmap='coolwarm') -> Option A
  4. Quick Check:

    Use c= for data colors and cmap= for colormap [OK]
Hint: Use c= for data, cmap= for colormap in plots [OK]
Common Mistakes:
  • Using color= instead of c= for data colors
  • Using color_map instead of cmap
  • Omitting c= when using cmap
3. What will be the output colormap type when running this code?
import matplotlib.pyplot as plt
import numpy as np

values = np.array([1, 2, 3, 4, 5])
plt.scatter(values, values, c=values, cmap='tab10')
plt.show()
medium
A. Sequential colormap
B. Diverging colormap
C. Qualitative colormap
D. No colormap applied

Solution

  1. Step 1: Identify the colormap used

    The colormap 'tab10' is a qualitative colormap designed for categorical data.
  2. Step 2: Understand the effect on scatter plot

    Even though values are numeric, using tab10 applies distinct colors for categories, so qualitative colormap is used.
  3. Final Answer:

    Qualitative colormap -> Option C
  4. Quick Check:

    tab10 = Qualitative colormap [OK]
Hint: tab10 means qualitative colormap [OK]
Common Mistakes:
  • Assuming tab10 is sequential
  • Thinking no colormap applied
  • Confusing diverging with qualitative
4. The following code throws an error. What is the mistake?
import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(-1, 1, 10)
y = x**2
plt.scatter(x, y, c=y, cmap='sequential')
plt.show()
medium
A. The variable y cannot be used for colors
B. x and y arrays have different lengths
C. Missing plt.colorbar() call
D. The colormap name 'sequential' is invalid

Solution

  1. Step 1: Check colormap name validity

    Matplotlib does not have a colormap named 'sequential'. It has specific names like 'viridis', 'plasma', etc.
  2. Step 2: Confirm other code parts

    Variables x and y have same length, y can be used for colors, and plt.colorbar() is optional.
  3. Final Answer:

    The colormap name 'sequential' is invalid -> Option D
  4. Quick Check:

    Invalid colormap name causes error [OK]
Hint: Use exact colormap names like 'viridis', not 'sequential' [OK]
Common Mistakes:
  • Using generic names like 'sequential' as colormap
  • Assuming plt.colorbar() is required
  • Thinking y can't be used for colors
5. You want to visualize temperature changes where values below zero are cold, above zero are warm, and zero is neutral. Which colormap type should you choose and why?
hard
A. Sequential, because temperature is ordered from low to high
B. Diverging, because data has a meaningful center at zero
C. Qualitative, because temperature categories are distinct
D. Random, to highlight all values differently

Solution

  1. Step 1: Analyze data characteristics

    Temperature data has a natural center point at zero separating cold and warm values.
  2. Step 2: Match colormap type to data

    Diverging colormaps highlight differences around a center value, perfect for zero-centered data.
  3. Final Answer:

    Diverging, because data has a meaningful center at zero -> Option B
  4. Quick Check:

    Center point data = Diverging colormap [OK]
Hint: Center value? Use diverging colormap [OK]
Common Mistakes:
  • Choosing sequential ignoring center zero
  • Using qualitative for numeric temperature
  • Thinking random colormap exists
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard