📖 Scenario: Imagine you are a data analyst presenting sales data to your team. You want to highlight important points on your sales chart so everyone understands the story behind the numbers.
🎯 Goal: You will create a simple sales line chart and add annotations to explain key points. This will help you learn how annotations make data easier to understand.
📋 What You'll Learn
Create a dictionary called monthly_sales with exact sales data for 6 months
Create a list called months with the month names in order
Create a matplotlib line plot of sales over months
Add at least two annotations to highlight important sales points
Display the plot with annotations
💡 Why This Matters
🌍 Real World
Annotations help explain charts in reports or presentations so viewers quickly understand key insights.
💼 Career
Data analysts and scientists use annotations to communicate findings clearly to non-technical audiences.
Progress0 / 4 steps
1
Create the sales data
Create a dictionary called monthly_sales with these exact entries: 'Jan': 150, 'Feb': 200, 'Mar': 180, 'Apr': 220, 'May': 210, 'Jun': 230. Also create a list called months with these exact values in order: 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun'.
Matplotlib
Hint
Use curly braces {} to create the dictionary and square brackets [] to create the list.
2
Prepare the sales values for plotting
Create a list called sales_values that contains the sales numbers from monthly_sales in the order of the months list.
Matplotlib
Hint
Use a list comprehension to get sales values in the order of months.
3
Create the sales line plot with annotations
Import matplotlib.pyplot as plt. Create a line plot using months on the x-axis and sales_values on the y-axis. Add two annotations: one at 'Feb' with text 'Sales jump' pointing to the point, and one at 'Apr' with text 'Peak sales' pointing to the point.
Matplotlib
Hint
Use plt.plot() to create the line chart and plt.annotate() to add text with arrows.
4
Display the plot
Use plt.show() to display the plot with the annotations.
Matplotlib
Hint
Call plt.show() to open the plot window.
Practice
(1/5)
1. Why do we use annotations in a matplotlib chart?
easy
A. To change the color of the chart background
B. To highlight important data points and explain their meaning
C. To increase the size of the chart window
D. To remove grid lines from the chart
Solution
Step 1: Understand the purpose of annotations and compare options
Annotations are used to add extra information directly on the chart to explain or highlight key points. Only To highlight important data points and explain their meaning describes highlighting and explaining data points, which matches the annotation purpose.
Final Answer:
To highlight important data points and explain their meaning -> Option B
Quick Check:
Annotations = Highlight key points [OK]
Hint: Annotations add notes to important points on charts [OK]
Common Mistakes:
Confusing annotations with styling options
Thinking annotations change chart size
Assuming annotations remove elements like grid lines
2. Which of the following is the correct syntax to add an annotation in matplotlib?
easy
A. plt.note('Note', position=(2, 3))
B. plt.annotation('Note', xy=(2, 3))
C. plt.add_annotation('Note', (2, 3))
D. plt.annotate('Note', xy=(2, 3))
Solution
Step 1: Recall matplotlib annotation function and check syntax correctness
The correct function to add annotations is plt.annotate(). plt.annotate('Note', xy=(2, 3)) uses plt.annotate with correct parameters 'Note' and xy coordinate tuple (2, 3).
Final Answer:
plt.annotate('Note', xy=(2, 3)) -> Option D
Quick Check:
Correct function is annotate() [OK]
Hint: Use plt.annotate() to add notes on plots [OK]
Common Mistakes:
Using plt.annotation instead of plt.annotate
Wrong parameter names or order
Confusing annotation with other plotting functions
A. A line plot with an annotation labeled 'Peak' pointing to point (3,6)
B. A scatter plot with a label 'Peak' at (2,5) without arrow
C. A line plot with no annotations visible
D. An error because arrowprops is missing required parameters
Solution
Step 1: Understand the plot and annotation code and analyze the annotation parameters
The code plots a line through points (1,4), (2,5), (3,6) and adds an annotation 'Peak' at (2,5) pointing to (3,6) with an arrow. xy=(3,6) is the point annotated, xytext=(2,5) is the text location, arrowprops adds an arrow. This matches A line plot with an annotation labeled 'Peak' pointing to point (3,6).
Final Answer:
A line plot with an annotation labeled 'Peak' pointing to point (3,6) -> Option A
Quick Check:
Annotation with arrow points to (3,6) [OK]
Hint: xy is point, xytext is label position in annotate() [OK]
Step 1: Check annotate parameter names and verify other parameters
plt.annotate requires xy= for the point to annotate. Here (2, 2) is passed as positional but should be xy=(2, 2). xytext=(3, 3) is correct, arrowprops is optional. Text 'Low' is present as first argument.
Final Answer:
The xy parameter should be named xy=(2, 2) -> Option A
Quick Check:
Use xy= keyword for point [OK]
Hint: Always use xy= for annotation point [OK]
Common Mistakes:
Passing xy as positional argument without keyword
Expecting arrow without arrowprops
Confusing xytext with xy
5. You want to highlight the maximum value in a dataset on a line chart using annotation. Which approach best tells the data story clearly?
hard
A. Change the line color only without annotation
B. Add annotation text at the bottom left corner of the plot without arrow
C. Annotate the max point with text and an arrow pointing to it, placed slightly above the point
D. Add annotation text overlapping the max point without arrow
Solution
Step 1: Understand effective annotation for max value and evaluate options for clarity
To tell the story, the annotation should clearly point to the max point with text and arrow, placed so it doesn't block the point. Annotate the max point with text and an arrow pointing to it, placed slightly above the point places text with arrow near max point, making it clear. Others either hide annotation or lack arrow, reducing clarity.
Final Answer:
Annotate the max point with text and an arrow pointing to it, placed slightly above the point -> Option C
Quick Check:
Clear annotation with arrow = best storytelling [OK]
Hint: Use arrow and text near key points for clarity [OK]
Common Mistakes:
Placing annotation far from data point
Not using arrows to connect text and point
Overlapping text on data point making it unclear
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.