0
0
Matplotlibdata~30 mins

Arrow annotations in Matplotlib - Mini Project: Build & Apply

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

Use plt.annotate() with xy for the arrow tip and xytext for the label position. Use arrowprops to draw the arrow.