Bird
Raised Fist0
Matplotlibdata~20 mins

Text annotations 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 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()
AA plot with points connected by a line and an annotation 'Square' pointing to (2,4) with an arrow starting at (3,7) in blue color.
BA plot with points connected by a line and an annotation 'Square' at (2,4) without any arrow.
CA plot with points connected by a line and an annotation 'Square' at (3,7) without any arrow.
DA plot with points connected by a line and an annotation 'Square' pointing to (3,7) with an arrow starting at (2,4) in red color.
Attempts:
2 left
💡 Hint
Look at the parameters xy and xytext in the annotate function and the color specified in arrowprops.
❓ data_output
intermediate
1: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()
A2
B3
C1
D0
Attempts:
2 left
💡 Hint
Each call to annotate adds one annotation regardless of arrow presence.
🔧 Debug
advanced
2: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()
ANo error, the plot shows annotation with a green arrow.
BSyntaxError due to incorrect dictionary syntax in arrowprops.
CKeyError because 'color' is not a valid key in arrowprops.
DTypeError because arrowprops must be a list, not a dict.
Attempts:
2 left
💡 Hint
Check the valid keys for arrowprops in matplotlib annotations.
❓ visualization
advanced
2: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()
AAn arrow with a double-headed style connecting (0,0) to (1,1).
BA simple straight arrow with a line and a triangular head connecting (0,0) to (1,1).
CNo arrow is drawn, only the text at (0,0).
DAn arrow with a filled triangular head and a curved tail connecting (0,0) to (1,1).
Attempts:
2 left
💡 Hint
The fancy arrow style has a filled head and a curved tail.
🧠 Conceptual
expert
1:30remaining
Understanding coordinate systems in annotations
In matplotlib annotations, what coordinate system does the xycoords='axes fraction' parameter refer to?
ACoordinates are given as a fraction of the axes width and height, ranging from 0 to 1.
BCoordinates are given in data units of the plotted data.
CCoordinates are given relative to the entire screen resolution.
DCoordinates are given in pixels relative to the figure window.
Attempts:
2 left
💡 Hint
Think about how fractions relate to the axes size.

Practice

(1/5)
1. What is the main purpose of adding text annotations in a matplotlib plot?
easy
A. To add labels or notes that explain or highlight data points
B. To change the color of the plot lines
C. To increase the size of the plot window
D. To save the plot as an image file

Solution

  1. Step 1: Understand what text annotations do

    Text annotations add extra information like labels or notes on specific parts of a plot.
  2. Step 2: Identify the correct purpose

    Changing colors or saving files are unrelated to annotations, which focus on explaining or highlighting data points.
  3. Final Answer:

    To add labels or notes that explain or highlight data points -> Option A
  4. Quick Check:

    Annotations = labels/notes [OK]
Hint: Annotations explain data points clearly on plots [OK]
Common Mistakes:
  • Confusing annotations with plot styling
  • Thinking annotations save or export plots
  • Assuming annotations change plot size
2. Which of the following is the correct syntax to add a text annotation at point (2, 3) with the text 'Peak' in matplotlib?
easy
A. plt.annotate(xy=(2, 3), text='Peak')
B. plt.text('Peak', xy=(2, 3))
C. plt.annotate('Peak', xy=(2, 3))
D. plt.text(x=2, y=3, label='Peak')

Solution

  1. Step 1: Recall the correct function and parameter order

    The function plt.annotate takes the text first, then the xy coordinate as a tuple.
  2. Step 2: Check each option

    plt.annotate('Peak', xy=(2, 3)) uses plt.annotate('Peak', xy=(2, 3)) which is correct. Others misuse parameter names or functions.
  3. Final Answer:

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

    annotate(text, xy=point) = correct syntax [OK]
Hint: Use plt.annotate('text', xy=(x,y)) for annotations [OK]
Common Mistakes:
  • Swapping text and xy parameters
  • Using plt.text with wrong arguments
  • Incorrect parameter names like label or text=
3. What will the following code display?
import matplotlib.pyplot as plt
plt.plot([1, 2, 3], [4, 5, 6])
plt.annotate('Max', xy=(3, 6), xytext=(2, 5), arrowprops=dict(facecolor='black'))
plt.show()
medium
A. A line plot with an annotation 'Max' at point (3,6) with an arrow pointing from (2,5)
B. A line plot with text 'Max' at (2,5) but no arrow
C. A line plot with an annotation 'Max' at (2,5) without arrow
D. Syntax error due to incorrect arrowprops usage

Solution

  1. Step 1: Understand plt.annotate parameters

    xy is the point to annotate (3,6), xytext is where the text appears (2,5), arrowprops adds an arrow from text to point.
  2. Step 2: Analyze the code output

    The plot shows a line, with 'Max' text at (2,5) and an arrow pointing to (3,6).
  3. Final Answer:

    A line plot with an annotation 'Max' at point (3,6) with an arrow pointing from (2,5) -> Option A
  4. Quick Check:

    annotate with xy, xytext, arrowprops = arrowed label [OK]
Hint: xy=point, xytext=text location, arrowprops=arrow [OK]
Common Mistakes:
  • Confusing xy and xytext roles
  • Expecting arrow without arrowprops
  • Thinking arrowprops causes syntax error
4. Identify the error in this code snippet:
import matplotlib.pyplot as plt
plt.plot([1, 2, 3], [4, 5, 6])
plt.annotate('High', (3, 6), xytext=(2, 5), arrowprops='black')
plt.show()
medium
A. plt.plot arguments are incorrect
B. arrowprops should be a dictionary, not a string
C. The text 'High' should be passed as xytext
D. xytext parameter is missing

Solution

  1. Step 1: Check arrowprops usage

    arrowprops must be a dictionary specifying arrow style, not a string.
  2. Step 2: Verify other parameters

    xytext is present, text is first argument, plot args are correct.
  3. Final Answer:

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

    arrowprops = dict(...) not string [OK]
Hint: arrowprops needs dict with arrow style, not string [OK]
Common Mistakes:
  • Passing arrowprops as string
  • Confusing text and xytext parameters
  • Incorrect plot data format
5. You want to highlight the lowest point on a plot with an annotation that includes an arrow and custom text offset. Which code snippet correctly achieves this for data points x=[1,2,3], y=[5,3,6]?
hard
A. plt.annotate('Lowest', xy=(2, 3), xytext=(2, 3), arrowprops=dict(color='blue'))
B. plt.annotate('Lowest', xytext=(2, 3), xy=(1, 4), arrowprops='red')
C. plt.text('Lowest', xy=(2, 3), xytext=(1, 4), arrowprops=dict(facecolor='red'))
D. plt.annotate('Lowest', xy=(2, 3), xytext=(1, 4), arrowprops=dict(facecolor='red'))

Solution

  1. Step 1: Identify the lowest point coordinates

    Lowest y is 3 at x=2, so xy=(2,3) is correct.
  2. Step 2: Check annotation parameters

    xytext=(1,4) offsets text for clarity, arrowprops uses dict with facecolor for arrow color.
  3. Step 3: Validate options

    plt.annotate('Lowest', xy=(2, 3), xytext=(1, 4), arrowprops=dict(facecolor='red')) matches correct syntax and logic. Others misuse parameters or functions.
  4. Final Answer:

    plt.annotate('Lowest', xy=(2, 3), xytext=(1, 4), arrowprops=dict(facecolor='red')) -> Option D
  5. Quick Check:

    Correct xy, xytext, arrowprops dict = plt.annotate('Lowest', xy=(2, 3), xytext=(1, 4), arrowprops=dict(facecolor='red')) [OK]
Hint: Use xy for point, xytext for label offset, arrowprops dict [OK]
Common Mistakes:
  • Swapping xy and xytext
  • Using plt.text instead of plt.annotate
  • Passing arrowprops as string
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