Challenge - 5 Problems
Discrete Colorbar Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate1:30remaining
Output of discrete colorbar with 3 colors
What will be the number of color segments in the colorbar produced by this code?
Matplotlib
import matplotlib.pyplot as plt import numpy as np from matplotlib.colors import BoundaryNorm cmap = plt.get_cmap('viridis', 3) bounds = [0, 1, 2, 3] norm = BoundaryNorm(bounds, cmap.N) fig, ax = plt.subplots(figsize=(4, 1)) fig.subplots_adjust(bottom=0.5) cb = plt.colorbar(plt.cm.ScalarMappable(norm=norm, cmap=cmap), cax=ax, boundaries=bounds, ticks=bounds) plt.show()
Attempts:
2 left
💡 Hint
Count the number of intervals defined by the bounds.
✗ Incorrect
The colorbar has 3 discrete colors because the bounds define 3 intervals: [0-1), [1-2), and [2-3).
❓ data_output
intermediate1:30remaining
Values mapped to colors using BoundaryNorm
Given the following code, what is the color index assigned to the value 2.5?
Matplotlib
import matplotlib.pyplot as plt from matplotlib.colors import BoundaryNorm bounds = [0, 1, 2, 3, 4] cmap = plt.get_cmap('plasma', 4) norm = BoundaryNorm(bounds, cmap.N) value = 2.5 color_index = norm(value) print(color_index)
Attempts:
2 left
💡 Hint
Check which interval 2.5 falls into between the bounds.
✗ Incorrect
The value 2.5 lies between 2 and 3, which is the third interval (index 2, zero-based).
❓ visualization
advanced2:00remaining
Identify the correct discrete colorbar visualization
Which option shows the correct discrete colorbar with 5 colors and boundaries at [0, 10, 20, 30, 40, 50]?
Matplotlib
import matplotlib.pyplot as plt import numpy as np from matplotlib.colors import BoundaryNorm cmap = plt.get_cmap('coolwarm', 5) bounds = [0, 10, 20, 30, 40, 50] norm = BoundaryNorm(bounds, cmap.N) fig, ax = plt.subplots(figsize=(6, 1)) fig.subplots_adjust(bottom=0.5) cb = plt.colorbar(plt.cm.ScalarMappable(norm=norm, cmap=cmap), cax=ax, boundaries=bounds, ticks=bounds) plt.show()
Attempts:
2 left
💡 Hint
Discrete colorbars show distinct color blocks matching the number of intervals.
✗ Incorrect
The code creates a discrete colorbar with 5 colors and boundaries exactly at the specified points, producing 5 distinct color blocks.
🔧 Debug
advanced1:30remaining
Error raised by incorrect BoundaryNorm usage
What error will this code raise?
Matplotlib
import matplotlib.pyplot as plt from matplotlib.colors import BoundaryNorm bounds = [0, 1, 2] cmap = plt.get_cmap('viridis', 3) norm = BoundaryNorm(bounds, 4) value = 1.5 color_index = norm(value) print(color_index)
Attempts:
2 left
💡 Hint
Check the relationship between cmap.N and the length of bounds.
✗ Incorrect
BoundaryNorm expects the number of colors (N) to be one less than the number of boundaries. Here, N=4 but bounds length is 3, causing a ValueError.
🚀 Application
expert3:00remaining
Creating a discrete colorbar for categorical data
You have 4 categories labeled 'A', 'B', 'C', 'D' and want a discrete colorbar with colors red, green, blue, and yellow respectively. Which code snippet correctly creates this discrete colorbar?
Attempts:
2 left
💡 Hint
Use ListedColormap with BoundaryNorm and set ticks at midpoints for labels.
✗ Incorrect
Option C correctly uses ListedColormap with BoundaryNorm and sets ticks at midpoints (0.5,1.5,2.5,3.5) to label categories properly. Other options either miss norm or have incorrect ticks.