📖 Scenario: You are working on a sales report for two different products over a week. You want to show their sales trends and also highlight their average sales with different styles on the same plot.
🎯 Goal: Build a line plot with two products' daily sales and add two separate legends: one for the sales lines and one for the average sales lines.
📋 What You'll Learn
Create two lists with daily sales data for Product A and Product B.
Create two variables for average sales of Product A and Product B.
Plot daily sales lines for both products.
Plot average sales lines for both products with a different style.
Add two separate legends: one for daily sales lines and one for average sales lines.
💡 Why This Matters
🌍 Real World
Multiple legends help show different groups of information clearly on one chart, like sales trends and averages.
💼 Career
Data scientists and analysts often need to present complex data visually with clear legends to help decision makers understand multiple data aspects.
Progress0 / 4 steps
1
Create sales data lists
Create two lists called sales_product_a and sales_product_b with these exact daily sales values: [10, 12, 15, 14, 13, 16, 18] for Product A and [8, 9, 11, 10, 12, 13, 15] for Product B.
Matplotlib
Hint
Use square brackets to create lists with the exact numbers given.
2
Calculate average sales
Create two variables called avg_a and avg_b that store the average sales of sales_product_a and sales_product_b respectively. Use the sum() and len() functions.
Matplotlib
Hint
Use sum() to add all values and len() to count the number of days.
3
Plot sales and average lines
Import matplotlib.pyplot as plt. Plot sales_product_a and sales_product_b as lines with labels 'Product A Sales' and 'Product B Sales'. Then plot horizontal lines for averages avg_a and avg_b with dashed style and labels 'Product A Average' and 'Product B Average'.
Matplotlib
Hint
Use plt.plot() for lines. Use 'r--' and 'g--' for dashed red and green lines.
4
Add two separate legends and show plot
Create two legends: one for the sales lines using plt.legend() with handles line1 and line2 placed at the upper left, and another for the average lines with handles line3 and line4 placed at the lower right. Then display the plot with plt.show().
Matplotlib
Hint
Use plt.legend(handles=[...], loc='...') to create each legend. Use plt.gca().add_artist() to add the first legend before adding the second.
Practice
(1/5)
1. What is the main reason to use multiple legends in a matplotlib plot?
easy
A. To increase the plot size automatically
B. To change the color of the plot lines
C. To label different groups or data sets separately on the same plot
D. To save the plot as an image file
Solution
Step 1: Understand the purpose of legends
Legends help identify what each line or marker in a plot represents.
Step 2: Recognize why multiple legends are needed
When a plot has multiple groups or categories, separate legends make it easier to understand each group distinctly.
Final Answer:
To label different groups or data sets separately on the same plot -> Option C
Quick Check:
Multiple legends = separate group labels [OK]
Hint: Multiple legends label different groups clearly [OK]
Common Mistakes:
Thinking legends change plot colors
Confusing legends with saving files
Assuming legends resize plots
2. Which matplotlib method is used to keep the first legend visible when adding a second legend?
easy
A. ax.add_artist()
B. plt.show()
C. plt.legend()
D. ax.set_title()
Solution
Step 1: Identify how legends are added
Legends are usually added with plt.legend() or ax.legend().
Step 2: Understand how to keep multiple legends
To keep the first legend visible after adding a second, you must add the first legend as an artist using ax.add_artist().
Final Answer:
ax.add_artist() -> Option A
Quick Check:
Keep first legend visible = add_artist() [OK]
Hint: Use add_artist() to keep first legend visible [OK]
5. You want to plot three data groups with separate legends placed at 'upper left', 'upper right', and 'lower left' without overlap. Which approach correctly achieves this?
hard
A. Plot all lines without legends and add a single legend manually
B. Call ax.legend() three times without add_artist(), changing loc each time
C. Use plt.legend() once with all labels combined
D. Create first legend, add it with ax.add_artist(), then create second and third legends with different loc values
Solution
Step 1: Understand legend placement
Legends placed at different loc positions avoid overlap visually.
Step 2: Use add_artist() to keep multiple legends
Adding the first legend as an artist keeps it visible when adding others.
Step 3: Create all three legends with distinct locations
Creating legends separately with loc set to 'upper left', 'upper right', and 'lower left' places them without overlap.
Final Answer:
Create first legend, add it with ax.add_artist(), then create second and third legends with different loc values -> Option D
Quick Check:
add_artist() + distinct loc = multiple legends no overlap [OK]
Hint: Add first legend as artist, set loc differently for others [OK]
Common Mistakes:
Calling legend() multiple times without add_artist()
Placing all legends at same location causing overlap
Using single legend for all groups losing clarity
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.