Bird
Raised Fist0
Matplotlibdata~20 mins

Annotation with arrows 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 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.

Practice

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

Solution

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

    The plt.annotate() function is used to add text annotations to a plot, often with arrows pointing to specific data points.
  2. Step 2: Identify the purpose of arrows in annotations

    Arrows help visually connect the annotation text to the exact point on the plot, making it clear what is being highlighted.
  3. Final Answer:

    To highlight specific points or features on the plot with text and arrows -> Option A
  4. Quick Check:

    Annotation with arrows = highlight points [OK]
Hint: Annotations with arrows point out key plot details [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 annotation with an arrow pointing to point (2, 3) with text 'Peak' in matplotlib?
easy
A. plt.annotation('Peak', xy=(2, 3), arrowprops=dict(color='black'))
B. plt.annotate('Peak', point=(2, 3), arrow=dict(color='black'))
C. plt.annotate('Peak', xy=(2, 3), arrowprops=dict(facecolor='black'))
D. plt.annotate('Peak', xy=(2, 3), arrowprops='black')

Solution

  1. Step 1: Check the correct function name and parameters

    The correct function is plt.annotate() with parameters text, xy for the point, and arrowprops as a dictionary for arrow style.
  2. Step 2: Validate the arrowprops dictionary format

    The arrow properties must be passed as a dictionary, e.g., dict(facecolor='black'). The other options have wrong parameter names or formats.
  3. Final Answer:

    plt.annotate('Peak', xy=(2, 3), arrowprops=dict(facecolor='black')) -> Option C
  4. Quick Check:

    Correct syntax uses xy and arrowprops dict [OK]
Hint: Use xy for point and arrowprops as dict for arrows [OK]
Common Mistakes:
  • Using wrong parameter names like point instead of xy
  • Passing arrowprops as string instead of dict
  • Misspelling annotate as annotation
3. What will the following code display?
import matplotlib.pyplot as plt
plt.plot([1, 2, 3], [2, 3, 5])
plt.annotate('Max', xy=(3, 5), xytext=(2, 4), arrowprops=dict(facecolor='red'))
plt.show()
medium
A. A line plot with an annotation 'Max' at (2,4) with a blue arrow
B. A line plot with an annotation 'Max' at (3,5) but no arrow
C. Syntax error due to wrong arrowprops
D. A line plot with an annotation 'Max' pointing from (2,4) to (3,5) with a red arrow

Solution

  1. Step 1: Understand the plot and annotation points

    The plot draws points (1,2), (2,3), (3,5). The annotation text 'Max' is placed at (2,4) with an arrow pointing to (3,5).
  2. Step 2: Check arrow properties and colors

    The arrow is styled with facecolor='red', so the arrow will be red. The annotation text is at (2,4), arrow points to (3,5).
  3. Final Answer:

    A line plot with an annotation 'Max' pointing from (2,4) to (3,5) with a red arrow -> Option D
  4. Quick Check:

    Annotation text at xytext, arrow points to xy [OK]
Hint: xytext is text position; xy is arrow tip [OK]
Common Mistakes:
  • Confusing xy and xytext positions
  • Assuming arrow color is blue by default
  • Thinking arrow is missing without xytext
4. Identify the error in this code snippet:
plt.plot([1, 2, 3], [4, 5, 6])
plt.annotate('Point', xy=(2, 5), arrowprops='black')
plt.show()
medium
A. plt.plot() requires color argument
B. arrowprops should be a dictionary, not a string
C. Missing xytext parameter for annotation text position
D. xy parameter should be a list, not a tuple

Solution

  1. Step 1: Check arrowprops parameter type

    The arrowprops parameter must be a dictionary describing arrow style, not a string.
  2. Step 2: Validate other parameters

    The xy parameter as a tuple is correct. xytext is optional and defaults to xy. plt.plot() does not require a color argument.
  3. Final Answer:

    arrowprops should be a dictionary, not a string -> Option B
  4. Quick Check:

    arrowprops = dict, not string [OK]
Hint: arrowprops must be dict, not string [OK]
Common Mistakes:
  • Passing arrowprops as string
  • Thinking xy must be list
  • Assuming xytext is mandatory
  • Believing plt.plot needs color always
5. You want to annotate the highest point in a dataset with an arrow and label 'Max Value'. Which code correctly finds the max point and adds the annotation with an arrow pointing to it?
hard
A. import matplotlib.pyplot as plt\nx = [1, 2, 3, 4]\ny = [2, 5, 1, 7]\nmax_idx = y.index(max(y))\nplt.plot(x, y)\nplt.annotate('Max Value', xy=(x[max_idx], y[max_idx]), xytext=(x[max_idx]-1, y[max_idx]+1), arrowprops=dict(facecolor='green'))\nplt.show()
B. import matplotlib.pyplot as plt\nx = [1, 2, 3, 4]\ny = [2, 5, 1, 7]\nmax_val = max(x)\nplt.plot(x, y)\nplt.annotate('Max Value', xy=(max_val, y[max_val]), xytext=(max_val-1, y[max_val]+1), arrowprops=dict(facecolor='green'))\nplt.show()
C. import matplotlib.pyplot as plt\nx = [1, 2, 3, 4]\ny = [2, 5, 1, 7]\nmax_idx = y.argmax()\nplt.plot(x, y)\nplt.annotate('Max Value', xy=(x[max_idx], y[max_idx]), xytext=(x[max_idx]-1, y[max_idx]+1), arrowprops=dict(facecolor='green'))\nplt.show()
D. import matplotlib.pyplot as plt\nx = [1, 2, 3, 4]\ny = [2, 5, 1, 7]\nmax_idx = y.index(max(y))\nplt.plot(x, y)\nplt.annotate('Max Value', xy=(x[max_idx], y[max_idx]), xytext=(x[max_idx]+1, y[max_idx]-1), arrowprops='green')\nplt.show()

Solution

  1. Step 1: Find the index of the maximum y value

    Using max_idx = y.index(max(y)) correctly finds the position of the highest y value in the list.
  2. Step 2: Use plt.annotate with correct arrowprops and positions

    The annotation text is placed near the max point with xytext offset, and arrowprops is a dictionary with facecolor='green'. This matches the correct syntax and logic.
  3. Final Answer:

    Uses y.index(max(y)) and arrowprops=dict(facecolor='green') -> Option A
  4. Quick Check:

    Find max index with list methods, arrowprops dict [OK]
Hint: Use y.index(max(y)) to find max point index [OK]
Common Mistakes:
  • Using max(x) instead of max(y) for max value
  • Using numpy method on list without import
  • Passing arrowprops as string instead of dict
  • Incorrect xytext offset causing overlap
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