0
0
Matplotlibdata~20 mins

Annotating specific points in Matplotlib - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Annotation Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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()
A(2, 4)
B(3, 6)
C(1, 2)
D(3, 5)
Attempts:
2 left
💡 Hint
The arrow points to the xy coordinate, not the text position.
data_output
intermediate
2: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()
A1
B3
C2
D4
Attempts:
2 left
💡 Hint
Count points where y is greater than 5.
🔧 Debug
advanced
2: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()
ANo error, plot shows annotation
BTypeError: arrowprops must be a dict
CValueError: xytext must be a tuple
DSyntaxError: invalid syntax
Attempts:
2 left
💡 Hint
Check the type of arrowprops argument.
visualization
advanced
2: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()
AThe text 'Peak' is placed 5 points above the point (2,4) with an arrow pointing to (2,4).
BThe text 'Peak' is placed at (2,5) in data coordinates with no arrow.
CThe text 'Peak' overlaps the point (2,4) with no offset or arrow.
DThe text 'Peak' is placed 5 units above (2,4) in data coordinates with an arrow.
Attempts:
2 left
💡 Hint
textcoords='offset points' means offset in points, not data units.
🚀 Application
expert
3: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()
A
max_val = max(y)
plt.annotate('Max Value', xy=(max_val, max_val), xytext=(max_val+1, max_val+1), arrowprops=dict(facecolor='red'))
B
max_idx = y.argmax()
plt.annotate('Max Value', xytext=(x[max_idx], y[max_idx]), xy=(x[max_idx]+1, y[max_idx]+1), arrowprops=dict(facecolor='red'))
C
max_idx = y.argmax()
plt.annotate('Max Value', xy=(x[max_idx], y[max_idx]), xytext=(x[max_idx]-1, y[max_idx]-1), arrowprops='->')
D
max_idx = y.argmax()
plt.annotate('Max Value', xy=(x[max_idx], y[max_idx]), xytext=(x[max_idx]+1, y[max_idx]+1), arrowprops=dict(facecolor='red'))
Attempts:
2 left
💡 Hint
Remember xy is the point annotated, xytext is text position, arrowprops must be dict.