Challenge - 5 Problems
Highlight and Annotate Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of highlighted region with annotation
What will be the output of this code snippet that highlights a region and adds an annotation on a matplotlib plot?
Matplotlib
import matplotlib.pyplot as plt import numpy as np x = np.linspace(0, 10, 100) y = np.sin(x) plt.plot(x, y) plt.axvspan(2, 4, color='yellow', alpha=0.3) plt.annotate('Peak region', xy=(3, np.sin(3)), xytext=(5, 0.5), arrowprops=dict(facecolor='black', shrink=0.05)) plt.show()
Attempts:
2 left
💡 Hint
Look at the parameters of axvspan and annotate carefully, especially the coordinates and colors.
✗ Incorrect
The code plots a sine wave, highlights the vertical region between x=2 and x=4 with a yellow transparent band, and adds an annotation labeled 'Peak region' with an arrow pointing from the text at (5, 0.5) to the point (3, sin(3)).
❓ data_output
intermediate1:30remaining
Number of annotations and highlighted regions
Given the following code, how many highlighted regions and annotations will appear on the plot?
Matplotlib
import matplotlib.pyplot as plt x = range(10) y = [i**2 for i in x] plt.plot(x, y) plt.axvspan(2, 4, color='green', alpha=0.2) plt.axvspan(6, 8, color='red', alpha=0.2) plt.annotate('Low range', xy=(3, 9), xytext=(1, 20), arrowprops=dict(facecolor='black')) plt.annotate('High range', xy=(7, 49), xytext=(8, 80), arrowprops=dict(facecolor='black')) plt.show()
Attempts:
2 left
💡 Hint
Count the number of axvspan and annotate calls with arrowprops.
✗ Incorrect
The code creates two vertical highlighted regions using axvspan and two annotations each with arrows pointing to specified points.
🔧 Debug
advanced1:30remaining
Identify the error in annotation coordinates
What error will this code produce when run, and why?
Matplotlib
import matplotlib.pyplot as plt x = [1, 2, 3] y = [4, 5, 6] plt.plot(x, y) plt.annotate('Point', xy=(4, 5), xytext=(2, 6), arrowprops=dict(facecolor='blue')) plt.show()
Attempts:
2 left
💡 Hint
Check if matplotlib allows annotations outside the data range.
✗ Incorrect
Matplotlib allows annotations outside the data range; no error occurs. The arrow points from xytext to xy as specified.
❓ visualization
advanced1:30remaining
Visual effect of alpha in highlighted region
What visual difference will changing alpha from 0.1 to 0.8 in axvspan produce on the plot?
Matplotlib
import matplotlib.pyplot as plt import numpy as np x = np.linspace(0, 5, 100) y = np.cos(x) plt.plot(x, y) plt.axvspan(1, 3, color='blue', alpha=0.1) plt.show()
Attempts:
2 left
💡 Hint
Alpha controls transparency; higher alpha means less transparent.
✗ Incorrect
Increasing alpha from 0.1 to 0.8 makes the shaded region more solid and less transparent, so it appears darker and more visible.
🚀 Application
expert3:00remaining
Highlight and annotate multiple peaks in data
Given a noisy sine wave, which code snippet correctly highlights the peaks between x=1 to 2 and x=4 to 5 with different colors and annotates them with arrows?
Matplotlib
import matplotlib.pyplot as plt import numpy as np x = np.linspace(0, 6, 300) y = np.sin(x) + 0.1 * np.random.randn(300) plt.plot(x, y) # Highlight and annotate peaks here plt.show()
Attempts:
2 left
💡 Hint
Use the noisy y values for annotation coordinates to match the plotted data points.
✗ Incorrect
Option A correctly uses axvspan to highlight vertical regions, uses the noisy y values at the correct indices for annotation points, and matches arrow colors to the highlight colors.