0
0
Matplotlibdata~30 mins

Annotation with arrows in Matplotlib - Mini Project: Build & Apply

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

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