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 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()
Attempts:
2 left
💡 Hint
Look at the parameters
xy and xytext in the annotate function and the color specified in arrowprops.✗ Incorrect
The
annotate function places the text at xytext coordinates (3,7) and draws an arrow pointing to xy coordinates (2,4). The arrow color is set to blue by facecolor='blue'.❓ data_output
intermediate1: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()
Attempts:
2 left
💡 Hint
Each call to
annotate adds one annotation regardless of arrow presence.✗ Incorrect
There are three calls to
annotate, so three annotations appear: 'Start' at (0,0), 'Middle' at (1,2), and 'End' at (2,5) with an arrow pointing to (2,4).🔧 Debug
advanced2: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()
Attempts:
2 left
💡 Hint
Check the valid keys for
arrowprops in matplotlib annotations.✗ Incorrect
The
arrowprops dictionary does not accept 'color' as a key. The correct key to set arrow color is facecolor or edgecolor. Using 'color' causes a KeyError.❓ visualization
advanced2: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()
Attempts:
2 left
💡 Hint
The
fancy arrow style has a filled head and a curved tail.✗ Incorrect
The
fancy arrow style draws an arrow with a filled triangular head and a curved tail, making it visually distinct from a simple arrow.🧠 Conceptual
expert1:30remaining
Understanding coordinate systems in annotations
In matplotlib annotations, what coordinate system does the
xycoords='axes fraction' parameter refer to?Attempts:
2 left
💡 Hint
Think about how fractions relate to the axes size.
✗ Incorrect
The
axes fraction coordinate system means coordinates are fractions of the axes dimensions, where (0,0) is bottom-left and (1,1) is top-right of the axes area.