0
0
Matplotlibdata~20 mins

Text annotations 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 with xytext and arrowprops
What will be the output of this code snippet that uses matplotlib to annotate a point with an arrow and custom text position?
Matplotlib
import matplotlib.pyplot as plt

fig, ax = plt.subplots()
ax.plot([1, 2, 3], [1, 4, 9])
ax.annotate('Square', xy=(2, 4), xytext=(3, 7), arrowprops=dict(facecolor='blue', shrink=0.05))
plt.show()
AA plot with points connected by a line and an annotation 'Square' pointing to (2,4) with an arrow starting at (3,7) in blue color.
BA plot with points connected by a line and an annotation 'Square' at (2,4) without any arrow.
CA plot with points connected by a line and an annotation 'Square' at (3,7) without any arrow.
DA plot with points connected by a line and an annotation 'Square' pointing to (3,7) with an arrow starting at (2,4) in red color.
Attempts:
2 left
💡 Hint
Look at the parameters xy and xytext in the annotate function and the color specified in arrowprops.
data_output
intermediate
1:30remaining
Number of annotations in a plot
Given the following code, how many annotations will be visible on the plot?
Matplotlib
import matplotlib.pyplot as plt

fig, ax = plt.subplots()
ax.plot([0, 1, 2], [0, 1, 4])
ax.annotate('Start', xy=(0, 0))
ax.annotate('Middle', xy=(1, 1), xytext=(1, 2))
ax.annotate('End', xy=(2, 4), xytext=(2, 5), arrowprops=dict(arrowstyle='->'))
plt.show()
A2
B3
C1
D0
Attempts:
2 left
💡 Hint
Each call to annotate adds one annotation regardless of arrow presence.
🔧 Debug
advanced
2:00remaining
Identify the error in annotation code
What error will this code raise when executed?
Matplotlib
import matplotlib.pyplot as plt

fig, ax = plt.subplots()
ax.plot([1, 2, 3], [1, 4, 9])
ax.annotate('Point', xy=(2, 4), xytext=(3, 7), arrowprops={'color': 'green'})
plt.show()
ANo error, the plot shows annotation with a green arrow.
BSyntaxError due to incorrect dictionary syntax in arrowprops.
CKeyError because 'color' is not a valid key in arrowprops.
DTypeError because arrowprops must be a list, not a dict.
Attempts:
2 left
💡 Hint
Check the valid keys for arrowprops in matplotlib annotations.
visualization
advanced
2:00remaining
Effect of different arrow styles in annotations
Which option shows the correct arrow style for the annotation with arrowstyle='fancy'?
Matplotlib
import matplotlib.pyplot as plt

fig, ax = plt.subplots()
ax.plot([0, 1], [0, 1])
ax.annotate('Fancy Arrow', xy=(1, 1), xytext=(0, 0), arrowprops=dict(arrowstyle='fancy'))
plt.show()
AAn arrow with a double-headed style connecting (0,0) to (1,1).
BA simple straight arrow with a line and a triangular head connecting (0,0) to (1,1).
CNo arrow is drawn, only the text at (0,0).
DAn arrow with a filled triangular head and a curved tail connecting (0,0) to (1,1).
Attempts:
2 left
💡 Hint
The fancy arrow style has a filled head and a curved tail.
🧠 Conceptual
expert
1:30remaining
Understanding coordinate systems in annotations
In matplotlib annotations, what coordinate system does the xycoords='axes fraction' parameter refer to?
ACoordinates are given as a fraction of the axes width and height, ranging from 0 to 1.
BCoordinates are given in data units of the plotted data.
CCoordinates are given relative to the entire screen resolution.
DCoordinates are given in pixels relative to the figure window.
Attempts:
2 left
💡 Hint
Think about how fractions relate to the axes size.