Challenge - 5 Problems
Annotation Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of annotation coordinates
What will be the coordinates of the annotation arrow tip in the plot after running this code?
Matplotlib
import matplotlib.pyplot as plt x = [1, 2, 3] y = [2, 4, 6] plt.scatter(x, y) plt.annotate('Point 2', xy=(2, 4), xytext=(3, 5), arrowprops=dict(facecolor='black')) plt.show()
Attempts:
2 left
💡 Hint
The arrow points to the xy coordinate, not the text position.
✗ Incorrect
The annotation arrow points to the xy parameter coordinates, which is (2, 4). The text is placed at xytext=(3, 5).
❓ data_output
intermediate2:00remaining
Number of annotations added
How many annotations will appear on the plot after running this code?
Matplotlib
import matplotlib.pyplot as plt x = [1, 2, 3, 4] y = [1, 4, 9, 16] plt.plot(x, y) for i in range(len(x)): if y[i] > 5: plt.annotate(f'High {y[i]}', xy=(x[i], y[i]), xytext=(x[i]+0.2, y[i]+1), arrowprops=dict(arrowstyle='->')) plt.show()
Attempts:
2 left
💡 Hint
Count points where y is greater than 5.
✗ Incorrect
Only y values 9 and 16 are greater than 5, so 2 annotations are added.
🔧 Debug
advanced2:00remaining
Identify the error in annotation code
What error will this code raise when run?
Matplotlib
import matplotlib.pyplot as plt plt.scatter([1, 2], [3, 4]) plt.annotate('Test', xy=(1, 3), xytext=(2, 5), arrowprops='->') plt.show()
Attempts:
2 left
💡 Hint
Check the type of arrowprops argument.
✗ Incorrect
arrowprops must be a dictionary describing arrow style, not a string.
❓ visualization
advanced2:00remaining
Effect of annotation with offset points
Which option best describes the visual effect of this annotation code on the plot?
Matplotlib
import matplotlib.pyplot as plt x = [0, 1, 2] y = [0, 1, 4] plt.plot(x, y, 'o-') plt.annotate('Peak', xy=(2, 4), xytext=(2, 5), textcoords='offset points', arrowprops=dict(arrowstyle='-|>')) plt.show()
Attempts:
2 left
💡 Hint
textcoords='offset points' means offset in points, not data units.
✗ Incorrect
Using textcoords='offset points' places text relative to the point in screen points, so text is 5 points above (2,4). Arrow points to (2,4).
🚀 Application
expert3:00remaining
Annotate maximum value in a dataset
Given a dataset, which code snippet correctly annotates the maximum y-value point with an arrow and label 'Max Value'?
Matplotlib
import matplotlib.pyplot as plt import numpy as np x = np.linspace(0, 10, 100) y = np.sin(x) + x/6 plt.plot(x, y) # Insert annotation code here plt.show()
Attempts:
2 left
💡 Hint
Remember xy is the point annotated, xytext is text position, arrowprops must be dict.
✗ Incorrect
Option D correctly finds max index, uses xy for point, xytext for label offset, and arrowprops as dict. Option D uses max value as coordinate incorrectly. Option D swaps xy and xytext. Option D uses string for arrowprops causing error.