📖 Scenario: You are working with data visualization to make charts easier to understand. Colors help show differences and patterns in data. Matplotlib has special color sets called colormaps. These colormaps come in three types: sequential, diverging, and qualitative.Sequential colormaps show data that goes from low to high. Diverging colormaps highlight values that move away from a middle point. Qualitative colormaps use different colors to show categories without order.
🎯 Goal: You will generate and print colors using a sequential colormap in Matplotlib. This will help you see how colors change with data and how to pick the right colormap for your chart.
📋 What You'll Learn
Create a list of numbers from 0 to 9
Create a variable for a sequential colormap name
Generate colors from the colormap using normalized values from the numbers
Print the list of colors generated
💡 Why This Matters
🌍 Real World
Choosing the right colormap helps make data charts clear and easy to understand. For example, showing temperature changes or survey categories with colors.
💼 Career
Data scientists and analysts use colormaps to highlight important patterns and differences in data visualizations for reports and presentations.
Progress0 / 4 steps
1
Create a list of numbers from 0 to 9
Create a list called numbers that contains the integers from 0 to 9 inclusive.
Matplotlib
Hint
Use the range() function to create numbers from 0 to 9, then convert it to a list.
2
Set a sequential colormap name
Create a variable called seq_cmap and set it to the string 'Blues', which is a sequential colormap in Matplotlib.
Matplotlib
Hint
Just assign the string 'Blues' to the variable seq_cmap.
3
Generate colors from the sequential colormap
Import matplotlib.pyplot as plt. Use plt.get_cmap(seq_cmap) to get the colormap. Then create a list called colors by applying the colormap to each number normalized by dividing by 9 (the max number) for each n in numbers.
Matplotlib
Hint
Use a list comprehension to apply the colormap to each normalized number.
4
Print the list of colors
Print the variable colors to see the RGBA color values generated from the sequential colormap.
Matplotlib
Hint
Use print(colors) to display the list of colors.
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
Step 1: Understand data order
Sequential colormaps are designed for data that goes from low to high in a smooth gradient.
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.
Final Answer:
Sequential colormap -> Option A
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
Step 1: Check scatter plot parameters
The parameter to set colors by data is c=, and colormap is set by cmap=.
Step 2: Identify correct syntax
plt.scatter(x, y, c=z, cmap='coolwarm') uses both c=z and cmap='coolwarm', which is correct syntax.
Final Answer:
plt.scatter(x, y, c=z, cmap='coolwarm') -> Option A
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
Step 1: Identify the colormap used
The colormap 'tab10' is a qualitative colormap designed for categorical data.
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.
Final Answer:
Qualitative colormap -> Option C
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
Step 1: Check colormap name validity
Matplotlib does not have a colormap named 'sequential'. It has specific names like 'viridis', 'plasma', etc.
Step 2: Confirm other code parts
Variables x and y have same length, y can be used for colors, and plt.colorbar() is optional.
Final Answer:
The colormap name 'sequential' is invalid -> Option D
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
Step 1: Analyze data characteristics
Temperature data has a natural center point at zero separating cold and warm values.
Step 2: Match colormap type to data
Diverging colormaps highlight differences around a center value, perfect for zero-centered data.
Final Answer:
Diverging, because data has a meaningful center at zero -> Option B
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 unknownstrongwhile earning +1 Grip toward your certificate.