📖 Scenario: You are creating a simple line chart to show sales over 5 months. You want to highlight the highest sales point with an arrow and a label.
🎯 Goal: Build a line plot of sales data and add an annotation with an arrow pointing to the highest sales point.
📋 What You'll Learn
Create a list called months with values: 'Jan', 'Feb', 'Mar', 'Apr', 'May'
Create a list called sales with values: 200, 450, 300, 500, 400
Create a variable called max_sales that stores the highest sales value
Create a variable called max_month that stores the month corresponding to the highest sales
Plot the sales data as a line chart with months on the x-axis and sales on the y-axis
Add an annotation with an arrow pointing to the highest sales point with the text 'Highest Sales'
💡 Why This Matters
🌍 Real World
Annotations with arrows help highlight important points in charts, making reports clearer and easier to understand.
💼 Career
Data analysts and scientists often use annotations to explain trends or outliers in data visualizations for stakeholders.
Progress0 / 4 steps
1
Create sales data lists
Create a list called months with these exact values: 'Jan', 'Feb', 'Mar', 'Apr', 'May'. Create a list called sales with these exact values: 200, 450, 300, 500, 400.
Matplotlib
Hint
Use square brackets to create lists and separate values with commas.
2
Find 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 matches the highest sales.
Matplotlib
Hint
Use the max() function to find the highest sales. Use list.index() to find the position of that value.
3
Plot sales data line chart
Import matplotlib.pyplot as plt. Plot the sales data as a line chart with months on the x-axis and sales on the y-axis. Use plt.plot(months, sales).
Matplotlib
Hint
Use import matplotlib.pyplot as plt to import plotting functions.
4
Add annotation with arrow and show plot
Add an annotation with plt.annotate() that shows the text 'Highest Sales' pointing to the highest sales point. Use xy=(max_month, max_sales) and xytext=('Apr', 550) for the arrow start. Use arrowprops=dict(arrowstyle='->'). 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 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
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.
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.
Final Answer:
To highlight specific points or features on the plot with text and arrows -> Option A
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
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.
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.
Final Answer:
plt.annotate('Peak', xy=(2, 3), arrowprops=dict(facecolor='black')) -> Option C
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
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
Step 1: Check arrowprops parameter type
The arrowprops parameter must be a dictionary describing arrow style, not a string.
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.
Final Answer:
arrowprops should be a dictionary, not a string -> Option B
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?
Using max_idx = y.index(max(y)) correctly finds the position of the highest y value in the list.
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.
Final Answer:
Uses y.index(max(y)) and arrowprops=dict(facecolor='green') -> Option A
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 unknownstrongwhile earning +1 Grip toward your certificate.