0
0
Matplotlibdata~10 mins

Legend outside the plot in Matplotlib - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Legend outside the plot
Create plot with data
Add legend inside plot
Adjust legend position
Place legend outside plot area
Display final plot
This flow shows how to create a plot, add a legend, then move the legend outside the plot area for better visibility.
Execution Sample
Matplotlib
import matplotlib.pyplot as plt

plt.plot([1, 2, 3], label='Line 1')
plt.plot([3, 2, 1], label='Line 2')
plt.legend(loc='center left', bbox_to_anchor=(1, 0.5))
plt.show()
This code plots two lines and places the legend outside the plot on the right side.
Execution Table
StepActionLegend LocationLegend CoordinatesPlot Displayed
1Plot line 1 with label 'Line 1'NoneNoneNo
2Plot line 2 with label 'Line 2'NoneNoneNo
3Add legend with default locationBest (inside plot)(default)No
4Change legend location to 'center left' outside plotcenter left(1, 0.5)No
5Show plot with legend outsidecenter left(1, 0.5)Yes
💡 Plot displayed with legend outside on the right at coordinates (1, 0.5)
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4Final
legend locationNoneNoneNonebest (inside)center leftcenter left
legend bbox_to_anchorNoneNoneNonedefault(1, 0.5)(1, 0.5)
plot displayedFalseFalseFalseFalseFalseTrue
Key Moments - 3 Insights
Why does the legend appear inside the plot before setting bbox_to_anchor?
By default, plt.legend() places the legend inside the plot at the 'best' location. This is shown in execution_table step 3 where the legend location is 'best (inside)'.
What does bbox_to_anchor=(1, 0.5) do to the legend position?
bbox_to_anchor sets the anchor point for the legend box. (1, 0.5) places it at the right center outside the plot area, as seen in step 4 and 5.
Why is the plot not displayed until plt.show() is called?
Matplotlib builds the plot step-by-step but only renders it on screen when plt.show() is called, as indicated by 'plot displayed' changing to True at step 5.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 4, where is the legend positioned?
AOutside the plot on the right center
BInside the plot at the best location
CAt the top left corner inside the plot
DAt the bottom center inside the plot
💡 Hint
Check the 'Legend Location' and 'Legend Coordinates' columns at step 4 in execution_table
According to variable_tracker, when does 'plot displayed' become True?
AAfter Step 3
BAfter Step 5
CAfter Step 4
DAt the start
💡 Hint
Look at the 'plot displayed' row in variable_tracker and see when it changes to True
If bbox_to_anchor was set to (0, 0), where would the legend appear relative to the plot?
AInside the plot at the center
BOutside the plot on the top right
COutside the plot on the bottom left
DInside the plot at the top left
💡 Hint
bbox_to_anchor sets the anchor point; (0,0) is bottom left outside the plot area
Concept Snapshot
plt.legend() adds a legend to a plot.
Use loc to set legend position.
bbox_to_anchor moves legend outside plot.
Example: loc='center left', bbox_to_anchor=(1, 0.5) places legend right outside.
Call plt.show() to display the plot with legend.
Full Transcript
This example shows how to place a legend outside a matplotlib plot. First, two lines are plotted with labels. By default, the legend appears inside the plot at the best location. Then, using plt.legend with loc='center left' and bbox_to_anchor=(1, 0.5), the legend moves outside the plot on the right center. The plot is displayed only after plt.show() is called. Variables like legend location and plot display status change step-by-step as shown in the tables.