📖 Scenario: Imagine you are analyzing sales data for a small store. You want to create a simple line chart showing sales over a week and add notes to highlight important days.
🎯 Goal: Build a line chart using matplotlib that shows sales data for 7 days. Add text annotations to highlight the highest and lowest sales days.
📋 What You'll Learn
Create a list called sales with exact values for 7 days
Create a list called days with day names from Monday to Sunday
Add a variable called max_sales to store the highest sales value
Add a variable called min_sales to store the lowest sales value
Use plt.plot() to draw the sales line chart
Use plt.annotate() to add text annotations for highest and lowest sales
Print the plot with plt.show()
💡 Why This Matters
🌍 Real World
Text annotations help highlight key points in charts, making data easier to understand for reports or presentations.
💼 Career
Data analysts and scientists often use annotations to explain trends or outliers in their visualizations.
Progress0 / 4 steps
1
Create sales and days data
Create a list called sales with these exact values: [150, 200, 170, 220, 180, 160, 210]. Also create a list called days with these exact strings: ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"].
Matplotlib
Hint
Use square brackets to create lists. Make sure the values and strings match exactly.
2
Find highest and lowest sales values
Create a variable called max_sales and set it to the maximum value in the sales list. Create another variable called min_sales and set it to the minimum value in the sales list.
Matplotlib
Hint
Use the max() and min() functions on the sales list.
3
Plot sales data and add annotations
Import matplotlib.pyplot as plt. Use plt.plot() to plot days on the x-axis and sales on the y-axis. Use plt.annotate() twice: once to label the highest sales point with text 'Highest Sales' and once to label the lowest sales point with text 'Lowest Sales'. Use the exact variables max_sales and min_sales to find the y-values. Use sales.index(max_sales) and sales.index(min_sales) to find the x positions.
Matplotlib
Hint
Use plt.plot() with days and sales. Use plt.annotate() with xy for the point and xytext for the label position. Add arrows with arrowprops.
4
Display the plot
Use plt.show() to display the plot with the sales line and annotations.
Matplotlib
Hint
Just call plt.show() to display the plot window.
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
Step 1: Understand what text annotations do
Text annotations add extra information like labels or notes on specific parts of a plot.
Step 2: Identify the correct purpose
Changing colors or saving files are unrelated to annotations, which focus on explaining or highlighting data points.
Final Answer:
To add labels or notes that explain or highlight data points -> Option A
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
Step 1: Recall the correct function and parameter order
The function plt.annotate takes the text first, then the xy coordinate as a tuple.
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.
Final Answer:
plt.annotate('Peak', xy=(2, 3)) -> Option C
Quick Check:
annotate(text, xy=point) = correct syntax [OK]
Hint: Use plt.annotate('text', xy=(x,y)) for annotations [OK]
B. arrowprops should be a dictionary, not a string
C. The text 'High' should be passed as xytext
D. xytext parameter is missing
Solution
Step 1: Check arrowprops usage
arrowprops must be a dictionary specifying arrow style, not a string.
Step 2: Verify other parameters
xytext is present, text is first argument, plot args are correct.
Final Answer:
arrowprops should be a dictionary, not a string -> Option B
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
Step 1: Identify the lowest point coordinates
Lowest y is 3 at x=2, so xy=(2,3) is correct.
Step 2: Check annotation parameters
xytext=(1,4) offsets text for clarity, arrowprops uses dict with facecolor for arrow color.
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.
Final Answer:
plt.annotate('Lowest', xy=(2, 3), xytext=(1, 4), arrowprops=dict(facecolor='red')) -> Option D