📖 Scenario: You are creating a simple line chart to compare sales of two products over 5 months. You want to add a legend to explain which line is which. The legend can be placed in different positions on the chart.
🎯 Goal: Learn how to add a legend to a matplotlib plot and place it in different positions using the loc parameter.
📋 What You'll Learn
Create a line plot with two lines representing sales data for Product A and Product B.
Add a legend with labels for each product.
Set the legend position using the loc parameter with a string value.
💡 Why This Matters
🌍 Real World
Legends help explain different data series in charts, making them easier to understand in reports and presentations.
💼 Career
Data scientists and analysts often create visualizations with legends to clearly communicate insights to stakeholders.
Progress0 / 4 steps
1
Create sales data and plot lines
Create two lists called months and sales_a with values [1, 2, 3, 4, 5] and [10, 15, 13, 17, 20] respectively. Also create a list called sales_b with values [12, 14, 16, 18, 22]. Then import matplotlib.pyplot as plt and plot sales_a and sales_b against months using plt.plot(). Use labels 'Product A' and 'Product B' for the lines.
Matplotlib
Hint
Use plt.plot(x_values, y_values, label='name') to plot lines with labels.
2
Add a legend with default placement
Add a legend to the plot by calling plt.legend() without any arguments.
Matplotlib
Hint
Call plt.legend() to add the legend with default position.
3
Change legend position to upper left
Modify the legend call to place the legend in the upper left corner by passing loc='upper left' to plt.legend().
Matplotlib
Hint
Use plt.legend(loc='upper left') to move the legend.
4
Display the plot
Add a line to display the plot by calling plt.show().
Matplotlib
Hint
Use plt.show() to display the plot window.
Practice
(1/5)
1. What does the loc parameter control in a matplotlib legend?
easy
A. The type of plot displayed
B. The color of the legend box
C. The font size of the legend text
D. The position of the legend inside or outside the plot area
Solution
Step 1: Understand the role of loc in legend placement
The loc parameter specifies where the legend appears relative to the plot, such as 'upper right' or 'lower left'.
Step 2: Differentiate from other legend properties
Other properties like color or font size are controlled by different parameters, not loc.
Final Answer:
The position of the legend inside or outside the plot area -> Option D
Quick Check:
Legend position = loc [OK]
Hint: Remember: loc sets legend position, not style [OK]
Common Mistakes:
Confusing loc with color or font size
Thinking loc controls plot type
Assuming loc changes data points
2. Which of the following is the correct syntax to place a legend at the lower center outside the plot using loc and bbox_to_anchor?
easy
A. plt.legend(loc='lower center', bbox_to_anchor=(0.5, -0.1))
B. plt.legend(loc='center lower', bbox_to_anchor=(0.5, -0.1))
C. plt.legend(location='lower center', bbox_to_anchor=(0.5, -0.1))
D. plt.legend(loc='lower center', bbox_to_anchor=[0.5, -0.1])
Solution
Step 1: Check correct parameter names and values
The parameter for legend position is loc, and the value 'lower center' is valid. The bbox_to_anchor expects a tuple for coordinates.
Step 2: Identify syntax errors in options
plt.legend(loc='center lower', bbox_to_anchor=(0.5, -0.1)) uses 'center lower' which is invalid. plt.legend(location='lower center', bbox_to_anchor=(0.5, -0.1)) uses 'location' instead of 'loc'. plt.legend(loc='lower center', bbox_to_anchor=[0.5, -0.1]) uses a list instead of a tuple for bbox_to_anchor.
Final Answer:
plt.legend(loc='lower center', bbox_to_anchor=(0.5, -0.1)) -> Option A
Quick Check:
Correct loc and tuple bbox_to_anchor = plt.legend(loc='lower center', bbox_to_anchor=(0.5, -0.1)) [OK]
Hint: Use loc='position' and bbox_to_anchor=(x, y) tuple [OK]
Common Mistakes:
Using wrong parameter name 'location' instead of 'loc'
A. Legend appears inside the plot at upper left corner
B. Legend appears outside the plot at the upper left corner
C. Legend appears outside the plot at the top-right corner
D. Code raises an error due to invalid bbox_to_anchor
Solution
Step 1: Understand loc and bbox_to_anchor interaction
The loc='upper left' sets the legend's anchor point to its upper left corner, and bbox_to_anchor=(1,1) places that anchor point at the top-right corner of the axes.
Step 2: Determine legend position
With the legend's upper left anchored at the plot's top-right corner (1,1), the legend extends to the right, appearing outside the plot at the top-right corner.
Final Answer:
Legend appears outside the plot at the top-right corner -> Option C