Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Highlight and Annotate Pattern
📖 Scenario: You are analyzing sales data for a small store. You want to see the sales trend over a week and highlight the day with the highest sales. This will help you understand when the store performs best.
🎯 Goal: Create a line chart of daily sales. Highlight the day with the highest sales using a different color and add an annotation to show the exact sales value on that day.
📋 What You'll Learn
Create a list called days with the days of the week.
Create a list called sales with the sales numbers for each day.
Find the day with the highest sales and store its index in max_index.
Plot the sales data as a line chart using matplotlib.
Highlight the highest sales day with a red dot.
Add an annotation showing the sales value on the highest sales day.
💡 Why This Matters
🌍 Real World
Highlighting important points in data charts helps businesses quickly see key information like best sales days.
💼 Career
Data analysts and scientists often highlight and annotate charts to communicate insights clearly to teams and managers.
Progress0 / 4 steps
1
Create sales data
Create a list called days with these exact values: 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'. Create another list called sales with these exact values: 150, 200, 170, 220, 180, 300, 250.
Matplotlib
Hint
Use square brackets to create lists. Put the days as strings in days and numbers in sales.
2
Find the highest sales day index
Create a variable called max_index and set it to the index of the highest value in the sales list using the sales.index() method and the max() function.
Matplotlib
Hint
Use max(sales) to get the highest sales number, then find its position with sales.index().
3
Plot sales and highlight highest day
Import matplotlib.pyplot as plt. Plot the sales data as a line chart using plt.plot(days, sales). Highlight the highest sales day by plotting a red dot at days[max_index] and sales[max_index] using plt.scatter() with color 'red' and size 100. Add an annotation with plt.annotate() showing the sales value at the highest sales day. Use the text f'Highest: {sales[max_index]}', position the text slightly above the red dot with xytext=(0,10) and textcoords='offset points'.
Matplotlib
Hint
Use plt.plot() for the line, plt.scatter() for the red dot, and plt.annotate() to add text above the dot.
4
Show the plot
Add plt.show() to display the plot with the highlighted highest sales day and annotation.
Matplotlib
Hint
Use plt.show() to display the chart window.
Practice
(1/5)
1. What is the main purpose of using highlight and annotate in a matplotlib plot?
easy
A. To save the plot as an image file
B. To change the color scheme of the entire plot
C. To draw attention to important parts and add notes explaining data points
D. To remove grid lines from the plot
Solution
Step 1: Understand the role of highlight
Highlighting is used to emphasize important areas on a graph to make them stand out.
Step 2: Understand the role of annotate
Annotations add notes with arrows to explain or give more information about specific data points.
Final Answer:
To draw attention to important parts and add notes explaining data points -> Option C
Quick Check:
Highlight + annotate = emphasize + explain [OK]
Hint: Highlight = focus, annotate = explain with notes [OK]
Common Mistakes:
Thinking highlight changes entire plot color
Confusing annotate with saving files
Assuming highlight removes grid lines
2. Which of the following is the correct syntax to add an annotation with an arrow pointing to point (2, 4) in matplotlib?
easy
A. plt.annotate('Note', xy=(2, 4), xytext=(3, 5), arrowprops=dict(facecolor='black'))
B. plt.annotate('Note', point=(2, 4), text=(3, 5), arrow=True)
C. plt.annotation('Note', xy=(2, 4), xytext=(3, 5), arrowprops=True)
D. plt.annotate('Note', xy=(2, 4), text=(3, 5), arrowprops=dict(color='red'))
Solution
Step 1: Check the function name and parameters
The correct function is plt.annotate with parameters xy for point and xytext for text location.
Step 2: Verify arrow properties
arrowprops must be a dictionary specifying arrow style, e.g., dict(facecolor='black').
Final Answer:
plt.annotate('Note', xy=(2, 4), xytext=(3, 5), arrowprops=dict(facecolor='black')) -> Option A
4. Identify the error in this code that tries to highlight and annotate a point:
import matplotlib.pyplot as plt
x = [1, 2, 3]
y = [5, 7, 9]
plt.plot(x, y)
plt.axhspan(6, 8, color='green')
plt.annotate('Important', xy=(2, 7), xytext=(2, 9), arrowprops='->')
plt.show()
medium
A. plt.axhspan should use x values, not y values
B. plt.plot should use scatter instead for annotation
C. xytext coordinates must be inside the plot range
D. arrowprops should be a dictionary, not a string
Solution
Step 1: Check plt.axhspan usage
plt.axhspan(6, 8, color='green') is correct to highlight horizontal area between y=6 and y=8.
Step 2: Check plt.annotate arrowprops parameter
arrowprops must be a dictionary describing arrow style, not a string like '->'.
Final Answer:
arrowprops should be a dictionary, not a string -> Option D
Quick Check:
arrowprops = dict(...) not string [OK]
Hint: arrowprops needs dict, not string like '->' [OK]
Common Mistakes:
Passing arrowprops as string instead of dict
Confusing axhspan with axvspan usage
Thinking xytext must be inside plot limits
5. You want to highlight the time period between 10 and 15 seconds on a line plot and annotate the highest point in that range with a label 'Max'. Which code snippet correctly achieves this?