0
0
Matplotlibdata~20 mins

Annotation with arrows 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 simple annotation with arrow
What will be the output of this code snippet that creates a plot with an annotation and an arrow?
Matplotlib
import matplotlib.pyplot as plt

plt.plot([1, 2, 3], [4, 5, 6])
plt.annotate('Point 2', xy=(2, 5), xytext=(3, 4), arrowprops=dict(facecolor='blue'))
plt.show()
AA plot with a line connecting points (1,4), (2,5), (3,6) and a blue arrow pointing from (3,4) to (2,5) with label 'Point 2'.
BA plot with a line connecting points (1,4), (2,5), (3,6) and a red arrow pointing from (2,5) to (3,4) with label 'Point 2'.
CA plot with a line connecting points (1,4), (2,5), (3,6) but no arrow or annotation visible.
DA plot with a line connecting points (1,4), (2,5), (3,6) and a blue arrow pointing from (2,5) to (3,4) with label 'Point 2'.
Attempts:
2 left
💡 Hint
Remember that xy is the point being annotated and xytext is where the text and arrow start.
data_output
intermediate
1:30remaining
Coordinates of annotated point and text
Given this code, what are the coordinates of the annotated point and the text position?
Matplotlib
import matplotlib.pyplot as plt

plt.plot([0, 1, 2], [0, 1, 4])
plt.annotate('Max', xy=(2, 4), xytext=(1, 3), arrowprops=dict(arrowstyle='->'))
plt.show()
AAnnotated point at (2, 4), text at (1, 3)
BAnnotated point at (1, 3), text at (2, 4)
CAnnotated point at (0, 0), text at (1, 3)
DAnnotated point at (2, 4), text at (2, 4)
Attempts:
2 left
💡 Hint
xy is the point being annotated, xytext is where the text is placed.
visualization
advanced
1:30remaining
Effect of different arrow styles
Which arrow style will produce a simple arrowhead with no tail or fancy shapes?
A'fancy'
B'wedge'
C'->'
D'-|>'
Attempts:
2 left
💡 Hint
Look for the simplest arrow style name.
🔧 Debug
advanced
2:00remaining
Identify the error in annotation code
What error will this code raise?
Matplotlib
import matplotlib.pyplot as plt
plt.plot([1, 2, 3], [4, 5, 6])
plt.annotate('Label', xy=(2, 5), xytext=(3, 4), arrowprops=dict(color='red'))
plt.show()
ASyntaxError due to missing colon
BNo error, plot shows with red arrow
CValueError: xy and xytext must be tuples of length 2
DTypeError: 'color' is not a valid key for arrowprops
Attempts:
2 left
💡 Hint
Check the valid keys for arrowprops in matplotlib.
🚀 Application
expert
2:30remaining
Create annotation with curved arrow
Which code snippet correctly creates an annotation with a curved arrow pointing from text at (1, 1) to point (2, 2)?
Aplt.annotate('Curved', xy=(1, 1), xytext=(2, 2), arrowprops=dict(arrowstyle='->', connectionstyle='arc3,rad=0.5'))
Bplt.annotate('Curved', xy=(2, 2), xytext=(1, 1), arrowprops=dict(arrowstyle='->', connectionstyle='arc3,rad=0.5'))
Cplt.annotate('Curved', xy=(2, 2), xytext=(1, 1), arrowprops=dict(arrowstyle='-|>', connectionstyle='arc,rad=0.5'))
Dplt.annotate('Curved', xy=(2, 2), xytext=(1, 1), arrowprops=dict(arrowstyle='->', connectionstyle='angle3,rad=0.5'))
Attempts:
2 left
💡 Hint
Remember xy is the point being annotated, xytext is text location. Use correct connectionstyle syntax.