Bird
Raised Fist0
Matplotlibdata~20 mins

Annotating specific points in Matplotlib - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Annotation Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of annotation coordinates
What will be the coordinates of the annotation arrow tip in the plot after running this code?
Matplotlib
import matplotlib.pyplot as plt
x = [1, 2, 3]
y = [2, 4, 6]
plt.scatter(x, y)
plt.annotate('Point 2', xy=(2, 4), xytext=(3, 5), arrowprops=dict(facecolor='black'))
plt.show()
A(2, 4)
B(3, 6)
C(1, 2)
D(3, 5)
Attempts:
2 left
💡 Hint
The arrow points to the xy coordinate, not the text position.
❓ data_output
intermediate
2:00remaining
Number of annotations added
How many annotations will appear on the plot after running this code?
Matplotlib
import matplotlib.pyplot as plt
x = [1, 2, 3, 4]
y = [1, 4, 9, 16]
plt.plot(x, y)
for i in range(len(x)):
    if y[i] > 5:
        plt.annotate(f'High {y[i]}', xy=(x[i], y[i]), xytext=(x[i]+0.2, y[i]+1), arrowprops=dict(arrowstyle='->'))
plt.show()
A1
B3
C2
D4
Attempts:
2 left
💡 Hint
Count points where y is greater than 5.
🔧 Debug
advanced
2:00remaining
Identify the error in annotation code
What error will this code raise when run?
Matplotlib
import matplotlib.pyplot as plt
plt.scatter([1, 2], [3, 4])
plt.annotate('Test', xy=(1, 3), xytext=(2, 5), arrowprops='->')
plt.show()
ANo error, plot shows annotation
BTypeError: arrowprops must be a dict
CValueError: xytext must be a tuple
DSyntaxError: invalid syntax
Attempts:
2 left
💡 Hint
Check the type of arrowprops argument.
❓ visualization
advanced
2:00remaining
Effect of annotation with offset points
Which option best describes the visual effect of this annotation code on the plot?
Matplotlib
import matplotlib.pyplot as plt
x = [0, 1, 2]
y = [0, 1, 4]
plt.plot(x, y, 'o-')
plt.annotate('Peak', xy=(2, 4), xytext=(2, 5), textcoords='offset points', arrowprops=dict(arrowstyle='-|>'))
plt.show()
AThe text 'Peak' is placed 5 points above the point (2,4) with an arrow pointing to (2,4).
BThe text 'Peak' is placed at (2,5) in data coordinates with no arrow.
CThe text 'Peak' overlaps the point (2,4) with no offset or arrow.
DThe text 'Peak' is placed 5 units above (2,4) in data coordinates with an arrow.
Attempts:
2 left
💡 Hint
textcoords='offset points' means offset in points, not data units.
🚀 Application
expert
3:00remaining
Annotate maximum value in a dataset
Given a dataset, which code snippet correctly annotates the maximum y-value point with an arrow and label 'Max Value'?
Matplotlib
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0, 10, 100)
y = np.sin(x) + x/6
plt.plot(x, y)
# Insert annotation code here
plt.show()
A
max_val = max(y)
plt.annotate('Max Value', xy=(max_val, max_val), xytext=(max_val+1, max_val+1), arrowprops=dict(facecolor='red'))
B
max_idx = y.argmax()
plt.annotate('Max Value', xytext=(x[max_idx], y[max_idx]), xy=(x[max_idx]+1, y[max_idx]+1), arrowprops=dict(facecolor='red'))
C
max_idx = y.argmax()
plt.annotate('Max Value', xy=(x[max_idx], y[max_idx]), xytext=(x[max_idx]-1, y[max_idx]-1), arrowprops='->')
D
max_idx = y.argmax()
plt.annotate('Max Value', xy=(x[max_idx], y[max_idx]), xytext=(x[max_idx]+1, y[max_idx]+1), arrowprops=dict(facecolor='red'))
Attempts:
2 left
💡 Hint
Remember xy is the point annotated, xytext is text position, arrowprops must be dict.

Practice

(1/5)
1. What is the main purpose of adding annotations to a matplotlib plot?
easy
A. To increase the size of the plot
B. To change the color of the plot lines
C. To add labels or notes to specific points on the plot
D. To remove grid lines from the plot

Solution

  1. Step 1: Understand what annotations do

    Annotations are used to add extra information like labels or notes to specific points on a plot.
  2. Step 2: Compare options with the purpose

    Only To add labels or notes to specific points on the plot describes adding labels or notes to points, which matches the purpose of annotations.
  3. Final Answer:

    To add labels or notes to specific points on the plot -> Option C
  4. Quick Check:

    Annotations = labels/notes on points [OK]
Hint: Annotations label points to explain data clearly [OK]
Common Mistakes:
  • Confusing annotations with changing plot colors
  • Thinking annotations resize the plot
  • Assuming annotations remove grid lines
2. Which of the following is the correct syntax to add an annotation at point (2, 5) with text 'Peak' in matplotlib?
easy
A. plt.annotate('Peak', xy=(2, 5))
B. plt.annotation('Peak', (2, 5))
C. plt.annotate('Peak', point=(2, 5))
D. plt.annotate(text='Peak', position=(2, 5))

Solution

  1. Step 1: Recall the correct function and parameters

    The correct function is plt.annotate with parameters: text first, then xy for the point coordinates.
  2. Step 2: Check each option's syntax

    plt.annotate('Peak', xy=(2, 5)) uses plt.annotate('Peak', xy=(2, 5)) which is correct. Others use wrong function names or parameter names.
  3. Final Answer:

    plt.annotate('Peak', xy=(2, 5)) -> Option A
  4. Quick Check:

    Correct syntax uses xy= for point [OK]
Hint: Use plt.annotate(text, xy=(x,y)) to label points [OK]
Common Mistakes:
  • Using plt.annotation instead of plt.annotate
  • Wrong parameter names like point or position
  • Swapping text and coordinate order
3. What will the following code display?
import matplotlib.pyplot as plt
x = [1, 2, 3]
y = [2, 5, 1]
plt.plot(x, y)
plt.annotate('High', xy=(2, 5), xytext=(3, 6), arrowprops=dict(facecolor='black'))
plt.show()
medium
A. A plot with a point at (3,6) labeled 'High' with an arrow pointing from (2,5)
B. Syntax error due to wrong arrowprops usage
C. A plot with no labels or arrows
D. A plot with a point at (2,5) labeled 'High' with an arrow pointing from (3,6)

Solution

  1. Step 1: Understand plt.annotate parameters

    xy=(2,5) is the point to annotate, xytext=(3,6) is where the text is placed, arrowprops adds an arrow from text to point.
  2. Step 2: Interpret the plot output

    The label 'High' appears at (3,6) with an arrow pointing to (2,5) on the plot line.
  3. Final Answer:

    A plot with a point at (2,5) labeled 'High' with an arrow pointing from (3,6) -> Option D
  4. Quick Check:

    xy = point, xytext = label position [OK]
Hint: xy=point, xytext=label position with arrow [OK]
Common Mistakes:
  • Confusing xy and xytext roles
  • Thinking arrow points from point to text
  • Assuming no arrow appears without extra code
4. Identify the error in this code snippet that tries to annotate point (4, 7):
plt.plot([1,2,3,4], [3,5,2,7])
plt.annotate('Max', point=(4,7))
plt.show()
medium
A. plt.show() must come before plt.annotate
B. The parameter 'point' is invalid; should use 'xy' instead
C. plt.plot needs labels before annotation
D. The text 'Max' should be a number

Solution

  1. Step 1: Check plt.annotate parameters

    The correct parameter for the point to annotate is 'xy', not 'point'.
  2. Step 2: Identify the error cause

    Using 'point' causes a TypeError because plt.annotate does not recognize it.
  3. Final Answer:

    The parameter 'point' is invalid; should use 'xy' instead -> Option B
  4. Quick Check:

    Use xy= for annotation point [OK]
Hint: Use xy=, not point=, to specify annotation location [OK]
Common Mistakes:
  • Using wrong parameter names like point or position
  • Placing plt.show() before annotation
  • Thinking annotation text must be numeric
5. You want to highlight the maximum value point on a plot with an annotation that places the label 10 units above the point and draws an arrow. Which code correctly achieves this for data x=[1,2,3], y=[4,9,7]?
hard
A. plt.plot(x, y)\nmax_idx = y.index(max(y))\nplt.annotate('Max', xy=(x[max_idx], y[max_idx]), xytext=(x[max_idx], y[max_idx]+10), arrowprops=dict(arrowstyle='->'))\nplt.show()
B. plt.plot(x, y)\nmax_idx = y.index(max(y))\nplt.annotate('Max', xytext=(x[max_idx], y[max_idx]), xy=(x[max_idx], y[max_idx]+10), arrowprops=dict(arrowstyle='->'))\nplt.show()
C. plt.plot(x, y)\nmax_val = max(y)\nplt.annotate('Max', xy=(max_val, x[max_idx]), xytext=(max_val, x[max_idx]+10), arrowprops=dict(arrowstyle='->'))\nplt.show()
D. plt.plot(x, y)\nmax_idx = y.index(max(y))\nplt.annotate('Max', xy=(x[max_idx], y[max_idx]), xytext=(x[max_idx], y[max_idx]-10), arrowprops=dict(arrowstyle='->'))\nplt.show()

Solution

  1. Step 1: Find the max value index correctly

    max_idx = y.index(max(y)) finds the index of the highest y value.
  2. Step 2: Set annotation point and label position

    xy is the max point (x[max_idx], y[max_idx]). xytext places label 10 units above: y[max_idx]+10.
  3. Step 3: Use arrowprops to draw arrow

    arrowprops=dict(arrowstyle='->') correctly adds an arrow from label to point.
  4. Final Answer:

    Code in option A correctly annotates max point with label 10 units above and arrow -> Option A
  5. Quick Check:

    xy=point, xytext=above point, arrowprops=arrow [OK]
Hint: Use xytext=(x, y+offset) to place label above point [OK]
Common Mistakes:
  • Swapping xy and xytext parameters
  • Using wrong coordinates for annotation
  • Placing label below instead of above
  • Mixing x and y values in annotation
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