0
0
Matplotlibdata~20 mins

Custom colormaps in Matplotlib - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Custom Colormap Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of Custom Colormap Creation
What is the output of this code snippet that creates a custom colormap and applies it to a simple gradient?
Matplotlib
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.colors import LinearSegmentedColormap

colors = [(1, 0, 0), (0, 1, 0), (0, 0, 1)]  # Red, Green, Blue
custom_cmap = LinearSegmentedColormap.from_list('mycmap', colors, N=3)
data = np.linspace(0, 1, 3).reshape(1, 3)
plt.imshow(data, cmap=custom_cmap)
plt.axis('off')
plt.show()
print(custom_cmap(0.5))
A[0.0, 0.5, 0.5, 1.0]
B[1.0, 0.0, 0.0, 1.0]
C[0.0, 1.0, 0.0, 1.0]
D[0.5, 0.5, 0.0, 1.0]
Attempts:
2 left
💡 Hint
Remember that the colormap interpolates colors between the given points. The value 0.5 is halfway between red and green in the list.
data_output
intermediate
1:30remaining
Number of Colors in Custom Colormap
Given this custom colormap created with N=5, how many distinct colors will it contain?
Matplotlib
from matplotlib.colors import LinearSegmentedColormap
colors = ['yellow', 'purple']
custom_cmap = LinearSegmentedColormap.from_list('custom', colors, N=5)
print(len(custom_cmap.colors) if hasattr(custom_cmap, 'colors') else 'No attribute colors')
A1
B2
C5
DNo attribute colors
Attempts:
2 left
💡 Hint
Check if the LinearSegmentedColormap object has a 'colors' attribute.
visualization
advanced
2:30remaining
Visualizing a Custom Diverging Colormap
Which option produces a plot with a smooth diverging colormap from blue to white to red?
Matplotlib
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.colors import LinearSegmentedColormap

colors = ['blue', 'white', 'red']
custom_cmap = LinearSegmentedColormap.from_list('diverging', colors, N=256)
data = np.outer(np.ones(100), np.linspace(-1, 1, 100))
plt.imshow(data, cmap=custom_cmap)
plt.colorbar()
plt.show()
AA plot with a gradient from blue on the left, white in the center, and red on the right
BA plot with a gradient from red on the left, white in the center, and blue on the right
CA plot with only blue color throughout
DA plot with only red color throughout
Attempts:
2 left
💡 Hint
The order of colors in the list defines the gradient direction.
🧠 Conceptual
advanced
2:00remaining
Understanding Colormap Normalization
If you create a custom colormap with colors ['black', 'yellow'] and apply it to data ranging from 0 to 100, what happens if you normalize the data with vmin=20 and vmax=80 before plotting?
AValues below 20 map to black, values above 80 map to yellow, and values between 20 and 80 map to colors between black and yellow
BValues below 20 map to yellow, values above 80 map to black
CAll values map to colors between black and yellow regardless of their range
DNormalization has no effect on the colormap application
Attempts:
2 left
💡 Hint
Normalization rescales data to the 0-1 range before applying the colormap.
🔧 Debug
expert
2:30remaining
Identify the Error in Custom Colormap Usage
What error will this code raise when trying to create and use a custom colormap?
Matplotlib
import matplotlib.pyplot as plt
from matplotlib.colors import LinearSegmentedColormap

colors = [(1, 0, 0), (0, 1)]  # Incorrect tuple length
custom_cmap = LinearSegmentedColormap.from_list('error_cmap', colors)
data = [[0, 1], [1, 0]]
plt.imshow(data, cmap=custom_cmap)
plt.show()
ATypeError: 'tuple' object cannot be interpreted as an integer
BValueError: color sequence must be RGB or RGBA tuples
CIndexError: tuple index out of range
DNo error, plot displays correctly
Attempts:
2 left
💡 Hint
Check the format of the color tuples in the list.