Bird
Raised Fist0
Matplotlibdata~20 mins

Arrow annotations in Matplotlib - Practice Problems & Coding Challenges

Choose your learning style10 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.

Practice

(1/5)
1. What is the main purpose of using plt.annotate() with arrow annotations in matplotlib?
easy
A. To create a new figure window
B. To change the color of the plot lines
C. To save the plot as an image file
D. To highlight and point to specific data points on a plot

Solution

  1. Step 1: Understand the role of plt.annotate()

    This function is used to add text annotations to plots, often with arrows pointing to data points.
  2. Step 2: Identify the purpose of arrow annotations

    Arrow annotations help visually highlight and point to specific points or areas on the plot for clarity.
  3. Final Answer:

    To highlight and point to specific data points on a plot -> Option D
  4. Quick Check:

    Arrow annotations = highlight points [OK]
Hint: Annotations with arrows point to important plot spots [OK]
Common Mistakes:
  • Confusing annotation with changing plot colors
  • Thinking annotation creates new figures
  • Assuming annotation saves files
2. Which of the following is the correct syntax to add an arrow annotation pointing to point (2, 3) with text at (3, 4)?
easy
A. plt.annotate('Note', xy=(2, 3), xytext=(3, 4), arrowprops=dict(arrowstyle='->'))
B. plt.annotate('Note', xytext=(2, 3), xy=(3, 4), arrowprops=dict(arrowstyle='->'))
C. plt.annotate('Note', xy=(3, 4), xytext=(2, 3), arrowprops=dict(arrowstyle='->'))
D. plt.annotate('Note', xytext=(3, 4), xy=(2, 3))

Solution

  1. Step 1: Recall plt.annotate() parameters

    xy is the point to annotate, xytext is the text location, and arrowprops defines the arrow style.
  2. Step 2: Match parameters to the question

    Text at (3,4) means xytext=(3,4), arrow points to (2,3) means xy=(2,3). Arrow style '->' is correct.
  3. Final Answer:

    plt.annotate('Note', xy=(2, 3), xytext=(3, 4), arrowprops=dict(arrowstyle='->')) -> Option A
  4. Quick Check:

    xy = point, xytext = text [OK]
Hint: Remember: xy=arrow tip, xytext=text location [OK]
Common Mistakes:
  • Swapping xy and xytext values
  • Omitting arrowprops for arrow display
  • Using wrong arrowstyle syntax
3. What will the following code display?
import matplotlib.pyplot as plt
plt.plot([1, 2, 3], [4, 5, 6])
plt.annotate('Peak', xy=(2, 5), xytext=(3, 6), arrowprops=dict(facecolor='red'))
plt.show()
medium
A. Syntax error due to missing arrowstyle in arrowprops
B. A line plot with a red arrow pointing from text at (3,6) to point (2,5) labeled 'Peak'
C. A line plot with no arrows, only text 'Peak' at (2,5)
D. A line plot with a blue arrow pointing from (2,5) to (3,6) labeled 'Peak'

Solution

  1. Step 1: Understand the plot and annotation

    The plot draws points (1,4), (2,5), (3,6). Annotation places text at (3,6) with an arrow pointing to (2,5).
  2. Step 2: Analyze arrowprops

    Arrow has facecolor='red', so arrow will be red. Arrowstyle defaults to '->' if not specified.
  3. Final Answer:

    A line plot with a red arrow pointing from text at (3,6) to point (2,5) labeled 'Peak' -> Option B
  4. Quick Check:

    Arrow color red, arrow points to xy [OK]
Hint: Arrow points from xytext to xy with given facecolor [OK]
Common Mistakes:
  • Thinking arrow points from xy to xytext
  • Assuming default arrow color is blue
  • Expecting syntax error without arrowstyle
4. Identify the error in this code snippet:
plt.plot([1, 2, 3], [4, 5, 6])
plt.annotate('Max', xy=(2, 5), xytext=(3, 6), arrowprops={'arrowstyle': '->'})
plt.show()
medium
A. arrowprops should be a dict, but it is given as a list
B. The code is correct and will run without errors
C. Missing import statement for matplotlib.pyplot
D. xy and xytext coordinates are reversed

Solution

  1. Step 1: Check code completeness

    The snippet uses plt but does not import matplotlib.pyplot as plt, which causes a NameError.
  2. Step 2: Verify arrowprops and coordinates

    arrowprops is correctly a dict, and coordinates are in correct order (xy=point, xytext=text).
  3. Final Answer:

    Missing import statement for matplotlib.pyplot -> Option C
  4. Quick Check:

    Import missing = error [OK]
Hint: Always import matplotlib.pyplot as plt before plotting [OK]
Common Mistakes:
  • Assuming arrowprops must be a list
  • Swapping xy and xytext
  • Ignoring missing imports
5. You want to annotate multiple points on a scatter plot with arrows pointing from text labels offset by (0.5, 0.5). Which code snippet correctly applies this for points in points = [(1,2), (3,4), (5,6)]?
hard
A. for x, y in points: plt.annotate(f'({x},{y})', xy=(x, y), xytext=(x+0.5, y+0.5), arrowprops=dict(arrowstyle='->'))
B. for x, y in points: plt.annotate(f'({x},{y})', xytext=(x, y), xy=(x+0.5, y+0.5), arrowprops=dict(arrowstyle='->'))
C. for x, y in points: plt.annotate(f'({x},{y})', xy=(x+0.5, y+0.5), xytext=(x, y), arrowprops=dict(arrowstyle='->'))
D. for x, y in points: plt.annotate(f'({x},{y})', xytext=(x+0.5, y+0.5), xy=(x, y))

Solution

  1. Step 1: Understand annotation parameters for multiple points

    Text should be offset by (0.5, 0.5) from each point, so xytext=(x+0.5, y+0.5) and arrow points to xy=(x, y).
  2. Step 2: Check arrowprops and loop structure

    Arrow style '->' is correct, and loop correctly iterates over points unpacking x and y.
  3. Final Answer:

    for x, y in points: plt.annotate(f'({x},{y})', xy=(x, y), xytext=(x+0.5, y+0.5), arrowprops=dict(arrowstyle='->')) -> Option A
  4. Quick Check:

    xy = point, xytext = offset text [OK]
Hint: xy is point, xytext is offset text location [OK]
Common Mistakes:
  • Swapping xy and xytext causing arrows to point wrong way
  • Omitting arrowprops so no arrow appears
  • Not unpacking points correctly in loop
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard