Challenge - 5 Problems
Colormap Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of applying 'viridis' colormap to a grayscale image
What is the shape and data type of the output array after applying the 'viridis' colormap to a 2D grayscale image using matplotlib's
cm.viridis?Matplotlib
import numpy as np import matplotlib.cm as cm gray_image = np.array([[0, 128], [192, 255]], dtype=np.uint8) colored_image = cm.viridis(gray_image / 255.0) print(colored_image.shape, colored_image.dtype)
Attempts:
2 left
💡 Hint
Remember that matplotlib colormaps return RGBA values as floats between 0 and 1.
✗ Incorrect
The 'viridis' colormap converts a 2D grayscale image into an RGBA image with shape (height, width, 4). The output dtype is float32 by default.
❓ data_output
intermediate1:30remaining
Number of unique colors after applying 'gray' colormap
Given a 1D numpy array with values [0, 0.5, 1.0], what is the number of unique colors in the output after applying the 'gray' colormap from matplotlib?
Matplotlib
import numpy as np import matplotlib.cm as cm values = np.array([0, 0.5, 1.0]) colors = cm.gray(values) unique_colors = np.unique(colors, axis=0) print(len(unique_colors))
Attempts:
2 left
💡 Hint
Check if the colormap maps different input values to distinct RGBA colors.
✗ Incorrect
Each input value maps to a distinct grayscale RGBA color, so there are 3 unique colors.
❓ visualization
advanced2:30remaining
Visualizing the effect of 'hot' colormap on a gradient image
Which option shows the correct matplotlib code to display a horizontal gradient image with the 'hot' colormap applied?
Attempts:
2 left
💡 Hint
A horizontal gradient image should have shape (1, width) and axis turned off for clean display.
✗ Incorrect
Option D correctly creates a 1-row gradient, applies 'hot' colormap, sets aspect ratio to auto, and hides axes.
🔧 Debug
advanced1:30remaining
Identify the error when applying colormap to integer image
What error will occur when running this code snippet?
import numpy as np import matplotlib.cm as cm img = np.array([[0, 255], [128, 64]], dtype=np.uint8) colored = cm.plasma(img) print(colored)
Attempts:
2 left
💡 Hint
Colormaps expect input values normalized between 0 and 1.
✗ Incorrect
The colormap expects float inputs between 0 and 1. Passing uint8 values causes a ValueError.
🚀 Application
expert3:00remaining
Combining two colormaps to create a custom colormap
Which code correctly creates a new colormap by combining the first half of 'Blues' and the second half of 'Reds' colormaps?
Attempts:
2 left
💡 Hint
Ensure the slices cover half of the arrays and the arrays have matching sizes before stacking.
✗ Incorrect
Option C correctly uses 256 samples, slices half (128) from each colormap, stacks them, and creates a new colormap.