0
0
Matplotlibdata~20 mins

Color mapping with colorbar in Matplotlib - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Color Mapping Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this matplotlib color mapping code?
Consider the following code that creates a scatter plot with color mapping and a colorbar. What will be the color of the point at x=2, y=2?
Matplotlib
import matplotlib.pyplot as plt
import numpy as np

x = np.array([1, 2, 3, 4])
y = np.array([1, 2, 3, 4])
colors = np.array([10, 20, 30, 40])

plt.scatter(x, y, c=colors, cmap='viridis')
plt.colorbar()
plt.close()  # Close plot to avoid display in test

color_at_point = plt.cm.viridis((20 - 10) / (40 - 10))
print(color_at_point)
A[0.127568, 0.566949, 0.550556, 1.0]
B[0.229739, 0.322361, 0.545706, 1.0]
C[0.993248, 0.906157, 0.143936, 1.0]
D[0.267004, 0.004874, 0.329415, 1.0]
Attempts:
2 left
💡 Hint
The color mapping normalizes values between the minimum and maximum of the data array before applying the colormap.
data_output
intermediate
1:30remaining
How many ticks will the colorbar show by default?
Given this code snippet that creates a scatter plot with a colorbar, how many tick marks will the colorbar display by default?
Matplotlib
import matplotlib.pyplot as plt
import numpy as np

x = np.arange(5)
y = np.arange(5)
colors = np.array([5, 10, 15, 20, 25])

sc = plt.scatter(x, y, c=colors, cmap='plasma')
cbar = plt.colorbar(sc)
plt.close()
A5
B6
C7
D4
Attempts:
2 left
💡 Hint
Matplotlib colorbars by default show 6 ticks unless specified otherwise.
visualization
advanced
2:30remaining
Which option produces a colorbar with discrete color segments?
You want to create a scatter plot with 4 distinct color segments in the colorbar, each representing a range of values. Which code snippet will produce this effect?
A
plt.scatter(x, y, c=colors, cmap='viridis')
plt.colorbar(ticks=[5, 10, 15, 20])
B
plt.scatter(x, y, c=colors, cmap='viridis', norm=plt.Normalize(vmin=5, vmax=20))
cbar = plt.colorbar(ticks=[5, 10, 15, 20])
C
plt.scatter(x, y, c=colors, cmap='viridis')
plt.colorbar(boundaries=[5, 10, 15, 20, 25])
D
from matplotlib.colors import BoundaryNorm
bounds = [5, 10, 15, 20, 25]
norm = BoundaryNorm(bounds, ncolors=256)
plt.scatter(x, y, c=colors, cmap='viridis', norm=norm)
plt.colorbar(ticks=bounds)
Attempts:
2 left
💡 Hint
Use BoundaryNorm to create discrete color segments matching boundaries.
🔧 Debug
advanced
1:30remaining
What error does this code raise?
Examine this code that attempts to create a scatter plot with a colorbar. What error will it raise when run?
Matplotlib
import matplotlib.pyplot as plt
import numpy as np

x = np.array([1, 2, 3])
y = np.array([1, 2, 3])
colors = ['red', 'green', 'blue']

plt.scatter(x, y, c=colors, cmap='viridis')
plt.colorbar()
plt.close()
AAttributeError: 'list' object has no attribute 'shape'
BTypeError: Invalid RGBA argument
CValueError: 'c' argument must be a sequence of numbers
DNo error, plot displays correctly
Attempts:
2 left
💡 Hint
The 'c' parameter expects numeric values when a colormap is specified.
🚀 Application
expert
3:00remaining
How to create a horizontal colorbar with label and custom ticks?
You want to create a scatter plot with a horizontal colorbar below the plot. The colorbar should have the label 'Intensity' and show ticks at 0, 0.5, and 1. Which code snippet achieves this?
A
sc = plt.scatter(x, y, c=colors, cmap='inferno')
cbar = plt.colorbar(sc, orientation='horizontal')
cbar.set_label('Intensity')
cbar.set_ticks([0, 0.5, 1])
B
sc = plt.scatter(x, y, c=colors, cmap='inferno')
cbar = plt.colorbar(sc)
cbar.set_label('Intensity')
cbar.set_ticks([0, 0.5, 1])
C
sc = plt.scatter(x, y, c=colors, cmap='inferno')
cbar = plt.colorbar(sc, orientation='horizontal')
cbar.set_label('Intensity')
cbar.set_ticks([1, 0.5, 0])
D
sc = plt.scatter(x, y, c=colors, cmap='inferno')
cbar = plt.colorbar(sc, orientation='vertical')
cbar.set_label('Intensity')
cbar.set_ticks([0, 0.5, 1])
Attempts:
2 left
💡 Hint
Set orientation='horizontal' in colorbar and use set_label and set_ticks methods.