📖 Scenario: You are creating a simple plot to show the sales trend of a product over four months. You want to highlight the highest sales point using an arrow annotation to make it clear to your audience.
🎯 Goal: Build a line plot of monthly sales and add an arrow annotation pointing to the highest sales value with a label.
📋 What You'll Learn
Create a list called months with values: 'Jan', 'Feb', 'Mar', 'Apr'
Create a list called sales with values: 150, 200, 250, 225
Create a variable called max_sales that stores the highest sales value
Create a variable called max_month that stores the month with the highest sales
Plot the sales data as a line plot with markers
Add an arrow annotation pointing to the highest sales point with the text 'Highest Sales'
Display the plot
💡 Why This Matters
🌍 Real World
Arrow annotations help highlight important data points in charts, making reports and presentations clearer and more engaging.
💼 Career
Data analysts and scientists often use annotations in visualizations to communicate insights effectively to stakeholders.
Progress0 / 4 steps
1
Create the sales data lists
Create a list called months with the values 'Jan', 'Feb', 'Mar', 'Apr'. Then create a list called sales with the values 150, 200, 250, 225.
Matplotlib
Hint
Use square brackets to create lists. Separate items with commas.
2
Find the highest sales and corresponding month
Create a variable called max_sales that stores the highest value in the sales list. Create a variable called max_month that stores the month from months that corresponds to max_sales.
Matplotlib
Hint
Use the max() function to find the highest sales. Use list.index() to find the position of that value.
3
Plot the sales data with markers
Import matplotlib.pyplot as plt. Use plt.plot() to plot months on the x-axis and sales on the y-axis with markers. Use marker='o' to show points.
Matplotlib
Hint
Use plt.plot() with marker='o' to show dots on the line.
4
Add arrow annotation and display the plot
Use plt.annotate() to add the text 'Highest Sales' pointing to the highest sales point. Use xy=(max_month, max_sales) for the arrow tip and xytext=('Mar', 180) for the text position. Use arrowprops=dict(arrowstyle='->') to draw the arrow. Finally, use plt.show() to display the plot.
Matplotlib
Hint
Use plt.annotate() with xy for the arrow tip and xytext for the label position. Use arrowprops to draw the 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
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.
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.
Final Answer:
To highlight and point to specific data points on a plot -> Option D
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
Step 1: Recall plt.annotate() parameters
xy is the point to annotate, xytext is the text location, and arrowprops defines the arrow style.
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.
Final Answer:
plt.annotate('Note', xy=(2, 3), xytext=(3, 4), arrowprops=dict(arrowstyle='->')) -> Option A
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
Step 1: Check code completeness
The snippet uses plt but does not import matplotlib.pyplot as plt, which causes a NameError.
Step 2: Verify arrowprops and coordinates
arrowprops is correctly a dict, and coordinates are in correct order (xy=point, xytext=text).
Final Answer:
Missing import statement for matplotlib.pyplot -> Option C
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
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).
Step 2: Check arrowprops and loop structure
Arrow style '->' is correct, and loop correctly iterates over points unpacking x and y.
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
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 unknownstrongwhile earning +1 Grip toward your certificate.