annotate adds text with an arrow pointing to a data point.
xy is the point to highlight, xytext is where the text appears.
Examples
This example highlights the highest point on the line with a red arrow and label 'Peak'.
Matplotlib
import matplotlib.pyplot as plt
x = [1, 2, 3, 4, 5]
y = [2, 3, 5, 7, 11]
plt.plot(x, y)
plt.annotate('Peak', xy=(5, 11), xytext=(3, 10), arrowprops=dict(facecolor='red'))
plt.show()
This example points to the value at x=3 with a blue arrow and label.
Matplotlib
import matplotlib.pyplot as plt
x = [0, 1, 2, 3, 4]
y = [1, 4, 9, 16, 25]
plt.plot(x, y)
plt.annotate('Square of 3', xy=(3, 9), xytext=(1, 20), arrowprops=dict(facecolor='blue'))
plt.show()
Sample Program
This program plots a simple line with points. It highlights the point at x=3, y=6 with a green arrow and label.
Matplotlib
import matplotlib.pyplot as plt
# Data for plotting
x = [1, 2, 3, 4, 5]
y = [2, 4, 6, 8, 10]
plt.plot(x, y, marker='o')
# Highlight the point where x=3
plt.annotate('Important Point', xy=(3, 6), xytext=(4, 7),
arrowprops=dict(facecolor='green', shrink=0.05))
plt.title('Highlight and Annotate Example')
plt.xlabel('X axis')
plt.ylabel('Y axis')
plt.grid(True)
plt.show()
OutputSuccess
Important Notes
You can customize arrow colors and styles using arrowprops.
Use xytext to place the annotation text where it does not block the data.
Adding marker='o' helps to see exact data points.
Summary
Use plt.annotate() to add labels and arrows to highlight data points.
Choose xy for the point and xytext for the label position.
Customize arrows with arrowprops for better visuals.
Practice
(1/5)
1. What is the main purpose of using highlight and annotate in a matplotlib plot?
easy
A. To save the plot as an image file
B. To change the color scheme of the entire plot
C. To draw attention to important parts and add notes explaining data points
D. To remove grid lines from the plot
Solution
Step 1: Understand the role of highlight
Highlighting is used to emphasize important areas on a graph to make them stand out.
Step 2: Understand the role of annotate
Annotations add notes with arrows to explain or give more information about specific data points.
Final Answer:
To draw attention to important parts and add notes explaining data points -> Option C
Quick Check:
Highlight + annotate = emphasize + explain [OK]
Hint: Highlight = focus, annotate = explain with notes [OK]
Common Mistakes:
Thinking highlight changes entire plot color
Confusing annotate with saving files
Assuming highlight removes grid lines
2. Which of the following is the correct syntax to add an annotation with an arrow pointing to point (2, 4) in matplotlib?
easy
A. plt.annotate('Note', xy=(2, 4), xytext=(3, 5), arrowprops=dict(facecolor='black'))
B. plt.annotate('Note', point=(2, 4), text=(3, 5), arrow=True)
C. plt.annotation('Note', xy=(2, 4), xytext=(3, 5), arrowprops=True)
D. plt.annotate('Note', xy=(2, 4), text=(3, 5), arrowprops=dict(color='red'))
Solution
Step 1: Check the function name and parameters
The correct function is plt.annotate with parameters xy for point and xytext for text location.
Step 2: Verify arrow properties
arrowprops must be a dictionary specifying arrow style, e.g., dict(facecolor='black').
Final Answer:
plt.annotate('Note', xy=(2, 4), xytext=(3, 5), arrowprops=dict(facecolor='black')) -> Option A
4. Identify the error in this code that tries to highlight and annotate a point:
import matplotlib.pyplot as plt
x = [1, 2, 3]
y = [5, 7, 9]
plt.plot(x, y)
plt.axhspan(6, 8, color='green')
plt.annotate('Important', xy=(2, 7), xytext=(2, 9), arrowprops='->')
plt.show()
medium
A. plt.axhspan should use x values, not y values
B. plt.plot should use scatter instead for annotation
C. xytext coordinates must be inside the plot range
D. arrowprops should be a dictionary, not a string
Solution
Step 1: Check plt.axhspan usage
plt.axhspan(6, 8, color='green') is correct to highlight horizontal area between y=6 and y=8.
Step 2: Check plt.annotate arrowprops parameter
arrowprops must be a dictionary describing arrow style, not a string like '->'.
Final Answer:
arrowprops should be a dictionary, not a string -> Option D
Quick Check:
arrowprops = dict(...) not string [OK]
Hint: arrowprops needs dict, not string like '->' [OK]
Common Mistakes:
Passing arrowprops as string instead of dict
Confusing axhspan with axvspan usage
Thinking xytext must be inside plot limits
5. You want to highlight the time period between 10 and 15 seconds on a line plot and annotate the highest point in that range with a label 'Max'. Which code snippet correctly achieves this?