0
0
Matplotlibdata~20 mins

Arrow annotations in Matplotlib - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Arrow Annotation Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of arrow annotation coordinates
What will be the coordinates of the arrow's start and end points in this plot code?
Matplotlib
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
ax.plot([0, 1], [0, 1])
arrow = ax.annotate('', xy=(0.8, 0.8), xytext=(0.2, 0.2), arrowprops=dict(facecolor='blue'))
print(f"Start: {arrow.xytext}, End: {arrow.xy}")
AStart: (0.8, 0.8), End: (0.2, 0.2)
BStart: (0.2, 0.8), End: (0.8, 0.2)
CStart: (0, 0), End: (1, 1)
DStart: (0.2, 0.2), End: (0.8, 0.8)
Attempts:
2 left
💡 Hint
Remember xytext is the start point and xy is the end point of the arrow.
data_output
intermediate
2:00remaining
Number of arrows in a plot
Given this code with multiple arrow annotations, how many arrows will be drawn on the plot?
Matplotlib
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
for i in range(3):
    ax.annotate('', xy=(i+1, i+1), xytext=(i, i), arrowprops=dict(facecolor='red'))
print(len(ax.texts))
A1
B6
C3
D0
Attempts:
2 left
💡 Hint
Each annotate call adds one arrow annotation.
visualization
advanced
3:00remaining
Effect of different arrow styles
Which option shows the correct arrow style applied to the annotation?
Matplotlib
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
ax.plot([0, 1], [0, 1])
ax.annotate('Point', xy=(0.7, 0.7), xytext=(0.3, 0.3), arrowprops=dict(arrowstyle='->', color='green'))
plt.show()
AArrow with a simple line and a triangular head pointing to the point
BArrow with a circular head and dashed line
CArrow with a double-headed style
DArrow with no head, just a line
Attempts:
2 left
💡 Hint
The arrowstyle '->' means a simple arrow with a triangular head.
🔧 Debug
advanced
2:00remaining
Identify the error in arrow annotation code
What error will this code produce when run?
Matplotlib
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
ax.annotate('Test', xy=(0.5, 0.5), xytext=(0.2, 0.2), arrowprops=dict(facecolor='red', shrink=1.5))
plt.show()
ASyntaxError due to missing colon
BTypeError: 'shrink' must be between 0 and 1
CNo error, plot shows arrow with shrink applied
DValueError: xy and xytext must be tuples
Attempts:
2 left
💡 Hint
Check the valid range for the 'shrink' parameter in arrowprops.
🚀 Application
expert
3:00remaining
Creating a custom curved arrow annotation
Which code snippet correctly creates a curved arrow annotation from (0.1, 0.1) to (0.9, 0.9) with a curved style?
Aax.annotate('', xy=(0.9, 0.9), xytext=(0.1, 0.1), arrowprops=dict(arrowstyle='->', connectionstyle='arc3,rad=0.3'))
Bax.annotate('', xy=(0.9, 0.9), xytext=(0.1, 0.1), arrowprops=dict(arrowstyle='-|>', connectionstyle='arc,rad=1.5'))
Cax.annotate('', xy=(0.1, 0.1), xytext=(0.9, 0.9), arrowprops=dict(arrowstyle='->', connectionstyle='arc3,rad=-0.3'))
Dax.annotate('', xy=(0.9, 0.9), xytext=(0.1, 0.1), arrowprops=dict(arrowstyle='->', connectionstyle='arc3,rad=3'))
Attempts:
2 left
💡 Hint
The connectionstyle 'arc3,rad=0.3' creates a gentle curved arrow.