📖 Scenario: You are creating a simple line chart to compare sales data for two products over four quarters. You want to make sure the legend explaining the lines is placed outside the main plot area so the chart looks clean and easy to read.
🎯 Goal: Build a line plot with two lines representing sales of two products. Place the legend outside the plot area on the right side.
📋 What You'll Learn
Create two lists named quarters and sales_product_a with exact values.
Create a list named sales_product_b with exact values.
Plot both sales lines with labels 'Product A' and 'Product B'.
Add a legend outside the plot on the right side using plt.legend with bbox_to_anchor and loc parameters.
Display the plot with plt.show().
💡 Why This Matters
🌍 Real World
Placing legends outside the plot is common in reports and dashboards to keep the chart area clean and readable.
💼 Career
Data analysts and scientists often need to create clear visualizations for presentations and reports, including customizing legend placement.
Progress0 / 4 steps
1
Create the sales data lists
Create a list called quarters with values ['Q1', 'Q2', 'Q3', 'Q4']. Create a list called sales_product_a with values [150, 200, 250, 300].
Matplotlib
Hint
Use square brackets to create lists with the exact values given.
2
Add sales data for Product B
Create a list called sales_product_b with values [180, 220, 260, 310].
Matplotlib
Hint
Make sure to use the exact list name and values.
3
Plot the sales lines with labels
Import matplotlib.pyplot as plt. Use plt.plot to plot quarters vs sales_product_a with label 'Product A'. Then plot quarters vs sales_product_b with label 'Product B'.
Matplotlib
Hint
Use plt.plot(x, y, label='name') to add lines with labels.
4
Place the legend outside the plot and show it
Use plt.legend with loc='center left' and bbox_to_anchor=(1, 0.5) to place the legend outside the plot on the right side. Then call plt.show() to display the plot.
Matplotlib
Hint
Use plt.legend(loc='center left', bbox_to_anchor=(1, 0.5)) to place the legend outside on the right.
Call plt.show() to display the plot.
Practice
(1/5)
1. What is the main purpose of placing a legend outside the plot in matplotlib?
easy
A. To keep the data area clear and improve readability
B. To make the plot smaller
C. To hide the legend from view
D. To change the plot colors automatically
Solution
Step 1: Understand legend placement
Legends inside the plot can cover data points, making the plot hard to read.
Step 2: Effect of placing legend outside
Placing the legend outside keeps the data visible and the plot clear.
Final Answer:
To keep the data area clear and improve readability -> Option A
Quick Check:
Legend outside = clear data area [OK]
Hint: Legend outside keeps plot data visible and neat [OK]
Common Mistakes:
Thinking legend outside hides it
Assuming it changes plot size automatically
Believing it changes plot colors
2. Which of the following is the correct way to place a legend outside the plot on the right using bbox_to_anchor and loc?
easy
A. plt.legend(loc='upper left', bbox_to_anchor=(-1, 1))
B. plt.legend(loc='center', bbox_to_anchor=(0, 0))
C. plt.legend(loc='lower left', bbox_to_anchor=(0.5, 0.5))
D. plt.legend(loc='upper right', bbox_to_anchor=(1.05, 1))
Solution
Step 1: Understand bbox_to_anchor coordinates
The tuple (1.05, 1) places the legend just outside the right edge at the top.
Step 2: Match loc with bbox_to_anchor
Using loc='upper right' anchors the legend's upper right corner to the point (1.05, 1).
Final Answer:
plt.legend(loc='upper right', bbox_to_anchor=(1.05, 1)) -> Option D
Quick Check:
Right outside = bbox_to_anchor > 1 on x [OK]
Hint: Use bbox_to_anchor >1 on x with loc='upper right' for right outside [OK]
Common Mistakes:
Using bbox_to_anchor inside plot area (less than 1)
Mismatching loc and bbox_to_anchor
Using negative bbox_to_anchor values incorrectly
3. What will be the effect of the following code snippet?
C. Invalid loc value 'outer_right' causes an error
D. No label provided for the plot
Solution
Step 1: Check loc parameter validity
Matplotlib does not support 'outer_right' as a valid loc string; valid locs include 'upper right', 'center left', etc.
Step 2: Effect of invalid loc
Using 'outer_right' causes a ValueError and the legend will not display.
Final Answer:
Invalid loc value 'outer_right' causes an error -> Option C
Quick Check:
loc must be valid string like 'upper right' [OK]
Hint: Use only valid loc strings like 'upper right', not 'outer_right' [OK]
Common Mistakes:
Using invalid loc strings
Ignoring need for plt.tight_layout()
Forgetting to add label to plot
5. You want to place a legend outside the plot on the right without overlapping the plot or cutting off the legend text. Which combination of commands is best to achieve this?
hard
A. plt.legend(loc='upper right', bbox_to_anchor=(0.5, 0.5)); plt.show()
B. plt.legend(loc='center left', bbox_to_anchor=(1, 0.5)); plt.tight_layout()
C. plt.legend(loc='lower left', bbox_to_anchor=(-0.5, 0)); plt.tight_layout()
D. plt.legend(loc='center', bbox_to_anchor=(1.5, 1.5)); plt.show()
Solution
Step 1: Choose correct legend position
loc='center left' with bbox_to_anchor=(1, 0.5) places the legend just outside the right center of the plot.
Step 2: Use plt.tight_layout() to avoid overlap
Calling plt.tight_layout() adjusts the plot layout to prevent the legend from overlapping or being cut off.
Final Answer:
plt.legend(loc='center left', bbox_to_anchor=(1, 0.5)); plt.tight_layout() -> Option B
Quick Check:
Right outside + tight_layout = no overlap [OK]
Hint: Use bbox_to_anchor outside + plt.tight_layout() to avoid overlap [OK]
Common Mistakes:
Not using plt.tight_layout() causing overlap
Placing legend inside plot by wrong bbox_to_anchor
Using bbox_to_anchor with negative values incorrectly
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.