Putting the legend outside the plot helps keep the plot area clear and easy to read. It avoids covering important data points.
Legend outside the plot in Matplotlib
plt.legend(loc='best', bbox_to_anchor=(x, y), borderaxespad=pad)loc sets the anchor point of the legend box.
bbox_to_anchor moves the legend box outside the plot by specifying coordinates.
plt.legend(loc='upper left', bbox_to_anchor=(1, 1))
plt.legend(loc='center left', bbox_to_anchor=(1, 0.5))
plt.legend(loc='lower left', bbox_to_anchor=(1, 0))
This code plots two lines and places the legend outside the plot area on the right side, centered vertically. The bbox_to_anchor moves the legend box outside the plot, and tight_layout() adjusts the plot to fit everything nicely.
import matplotlib.pyplot as plt x = [1, 2, 3, 4] y1 = [10, 20, 25, 30] y2 = [15, 18, 22, 27] plt.plot(x, y1, label='Line 1') plt.plot(x, y2, label='Line 2') # Put legend outside the plot on the right plt.legend(loc='center left', bbox_to_anchor=(1, 0.5)) plt.title('Plot with Legend Outside') plt.xlabel('X axis') plt.ylabel('Y axis') plt.tight_layout() # Adjust layout to make room for legend plt.show()
Use plt.tight_layout() or adjust figure size to prevent clipping of the legend.
You can change bbox_to_anchor coordinates to move the legend to different outside positions.
Legends can be placed outside the plot to keep the plot area clear.
Use loc and bbox_to_anchor together to position the legend outside.
Adjust layout with tight_layout() to avoid overlap or clipping.
Practice
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 AQuick Check:
Legend outside = clear data area [OK]
- Thinking legend outside hides it
- Assuming it changes plot size automatically
- Believing it changes plot colors
bbox_to_anchor and loc?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 DQuick Check:
Right outside = bbox_to_anchor > 1 on x [OK]
- Using bbox_to_anchor inside plot area (less than 1)
- Mismatching loc and bbox_to_anchor
- Using negative bbox_to_anchor values incorrectly
import matplotlib.pyplot as plt plt.plot([1, 2, 3], label='Line') plt.legend(loc='center left', bbox_to_anchor=(1, 0.5)) plt.show()
Solution
Step 1: Analyze bbox_to_anchor coordinates
The point (1, 0.5) is at the right edge horizontally and center vertically.Step 2: Understand loc='center left'
The legend's center left side is anchored at (1, 0.5), placing it just outside the plot on the right center.Final Answer:
Legend appears outside the plot on the right center -> Option AQuick Check:
loc='center left' + bbox_to_anchor=(1,0.5) = right outside center [OK]
- Thinking legend is inside plot
- Confusing loc and bbox_to_anchor roles
- Expecting legend below plot
import matplotlib.pyplot as plt plt.plot([1, 2, 3], label='Data') plt.legend(loc='outer_right', bbox_to_anchor=(1.05, 1)) plt.show()
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 CQuick Check:
loc must be valid string like 'upper right' [OK]
- Using invalid loc strings
- Ignoring need for plt.tight_layout()
- Forgetting to add label to plot
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 BQuick Check:
Right outside + tight_layout = no overlap [OK]
- Not using plt.tight_layout() causing overlap
- Placing legend inside plot by wrong bbox_to_anchor
- Using bbox_to_anchor with negative values incorrectly
Ready to prove it?
Pass the test and this pattern moves unknown
