0
0
Matplotlibdata~20 mins

Discrete colorbars in Matplotlib - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Discrete Colorbar Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
1: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()
A3
B4
C2
D1
Attempts:
2 left
💡 Hint
Count the number of intervals defined by the bounds.
data_output
intermediate
1: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)
A0
B2
C3
D1
Attempts:
2 left
💡 Hint
Check which interval 2.5 falls into between the bounds.
visualization
advanced
2: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()
AColorbar with 6 color blocks and boundaries at 0, 8, 16, 24, 32, 40, 48
BColorbar with smooth gradient blending colors continuously from 0 to 50
CColorbar with 4 color blocks and boundaries at 0, 12, 24, 36, 48
DColorbar with 5 distinct color blocks separated by clear boundaries at 0,10,20,30,40,50
Attempts:
2 left
💡 Hint
Discrete colorbars show distinct color blocks matching the number of intervals.
🔧 Debug
advanced
1: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)
AValueError: Number of colors, N, must be one less than number of boundaries
BTypeError: 'BoundaryNorm' object is not callable
CIndexError: color index out of range
DNo error, prints 1
Attempts:
2 left
💡 Hint
Check the relationship between cmap.N and the length of bounds.
🚀 Application
expert
3: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?
A
import matplotlib.pyplot as plt
from matplotlib.colors import ListedColormap

colors = ['red', 'green', 'blue', 'yellow']
cmap = ListedColormap(colors)

fig, ax = plt.subplots(figsize=(6, 1))
fig.subplots_adjust(bottom=0.5)
cb = plt.colorbar(plt.cm.ScalarMappable(cmap=cmap), cax=ax)
cb.set_ticks([0,1,2,3])
cb.ax.set_xticklabels(['A', 'B', 'C', 'D'])
plt.show()
B
import matplotlib.pyplot as plt
from matplotlib.colors import BoundaryNorm

colors = ['red', 'green', 'blue', 'yellow']
cmap = plt.get_cmap('tab10', 4)
bounds = [0, 1, 2, 3, 4]
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()
C
import matplotlib.pyplot as plt
from matplotlib.colors import ListedColormap, BoundaryNorm

colors = ['red', 'green', 'blue', 'yellow']
cmap = ListedColormap(colors)
bounds = [0, 1, 2, 3, 4]
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=[0.5,1.5,2.5,3.5])
cb.ax.set_xticklabels(['A', 'B', 'C', 'D'])
plt.show()
D
import matplotlib.pyplot as plt
from matplotlib.colors import ListedColormap, BoundaryNorm

colors = ['red', 'green', 'blue', 'yellow']
cmap = ListedColormap(colors)
bounds = [0, 1, 2, 3, 4]
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)
cb.ax.set_xticklabels(['A', 'B', 'C', 'D'])
plt.show()
Attempts:
2 left
💡 Hint
Use ListedColormap with BoundaryNorm and set ticks at midpoints for labels.