Challenge - 5 Problems
Color Mapping Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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)
Attempts:
2 left
💡 Hint
The color mapping normalizes values between the minimum and maximum of the data array before applying the colormap.
✗ Incorrect
The color value 20 is normalized between 10 (min) and 40 (max) as (20-10)/(40-10) = 0.3333. The viridis colormap at 0.3333 corresponds to the RGBA color approximately [0.127568, 0.566949, 0.550556, 1.0].
❓ data_output
intermediate1: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()
Attempts:
2 left
💡 Hint
Matplotlib colorbars by default show 6 ticks unless specified otherwise.
✗ Incorrect
By default, matplotlib colorbars use 6 ticks to label the color scale, regardless of the number of points plotted.
❓ visualization
advanced2: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?
Attempts:
2 left
💡 Hint
Use BoundaryNorm to create discrete color segments matching boundaries.
✗ Incorrect
BoundaryNorm with specified bounds creates discrete color segments. The colorbar ticks correspond to these boundaries, showing distinct color blocks.
🔧 Debug
advanced1: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()
Attempts:
2 left
💡 Hint
The 'c' parameter expects numeric values when a colormap is specified.
✗ Incorrect
When using a colormap, the 'c' argument must be numeric. Passing color names as strings causes ValueError.
🚀 Application
expert3: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?
Attempts:
2 left
💡 Hint
Set orientation='horizontal' in colorbar and use set_label and set_ticks methods.
✗ Incorrect
Option A correctly sets the colorbar orientation to horizontal, adds the label 'Intensity', and sets ticks at 0, 0.5, and 1 in ascending order.