0
0
Matplotlibdata~30 mins

Text annotations in Matplotlib - Mini Project: Build & Apply

Choose your learning style9 modes available
Text Annotations with Matplotlib
📖 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
Need a 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
Need a 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
Need a 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
Need a hint?

Just call plt.show() to display the plot window.