Challenge - 5 Problems
Custom Colormap Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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))
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.
✗ Incorrect
The custom colormap has three colors: red at 0.0, green at 0.5, and blue at 1.0. The value 0.5 corresponds exactly to green, so the output color is green with full opacity.
❓ data_output
intermediate1: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')
Attempts:
2 left
💡 Hint
Check if the LinearSegmentedColormap object has a 'colors' attribute.
✗ Incorrect
The LinearSegmentedColormap object does not have a 'colors' attribute. The N parameter controls the resolution internally but does not create a list attribute named 'colors'.
❓ visualization
advanced2: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()
Attempts:
2 left
💡 Hint
The order of colors in the list defines the gradient direction.
✗ Incorrect
The colormap is created from blue to white to red, so the gradient goes from blue on the low end to red on the high end with white in the middle.
🧠 Conceptual
advanced2: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?
Attempts:
2 left
💡 Hint
Normalization rescales data to the 0-1 range before applying the colormap.
✗ Incorrect
Normalization with vmin=20 and vmax=80 rescales data so that 20 maps to 0 (black) and 80 maps to 1 (yellow). Values outside this range are clipped to the nearest color.
🔧 Debug
expert2: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()
Attempts:
2 left
💡 Hint
Check the format of the color tuples in the list.
✗ Incorrect
The second color tuple has only two elements instead of three or four, causing a ValueError when matplotlib tries to interpret it as RGB or RGBA.