To change the position of a legend in Matplotlib, you can use the plt.legend() function.
For example, you can use the following syntax to place the legend in the upper left corner of the plot:
plt.legend(loc='upper left')
The default location is “best” – which is where Matplotlib automatically finds a location for the legend based on where it avoids covering any data points.
However, you can specify any of the following legend locations:
- upper right
- upper left
- lower left
- lower right
- right
- center left
- center right
- lower center
- upper center
- center
You can also use the bbox_to_anchor() argument to place the legend outside of the plot. For example, you can use the following syntax to place the legend in the top right corner outside of the plot:
plt.legend(bbox_to_anchor=(1.05, 1), loc='upper left', borderaxespad=0)
The following examples show how to use each of these methods in practice.
Example 1: Change Legend Position Inside of Matplotlib Plot
The following code shows how to place the legend inside the center right portion of a Matplotlib line plot:
import pandas as pd import matplotlib.pyplot as plt #create data df = pd.DataFrame({'points': [11, 17, 16, 18, 22, 25, 26, 24, 29], 'assists': [5, 7, 7, 9, 12, 9, 9, 4, 8]}) #add lines to plot plt.plot(df['points'], label='Points', color='green') plt.plot(df['assists'], label='Assists', color='steelblue') #place legend in center right of plot plt.legend(loc='center right', title='Metric')
And the following code shows how to place the legend inside the upper left portion of a Matplotlib plot:
import pandas as pd import matplotlib.pyplot as plt #create data df = pd.DataFrame({'points': [11, 17, 16, 18, 22, 25, 26, 24, 29], 'assists': [5, 7, 7, 9, 12, 9, 9, 4, 8]}) #add lines to plot plt.plot(df['points'], label='Points', color='green') plt.plot(df['assists'], label='Assists', color='steelblue') #place legend in center right of plot plt.legend(loc='upper left', title='Metric')
Example 2: Change Legend Position Outside of Matplotlib Plot
To place the legend outside of a Matplotlib plot, we can use the bbox_to_anchor() argument.
For example, here’s how to place the legend outside the top right corner of the plot:
import pandas as pd import matplotlib.pyplot as plt #create data df = pd.DataFrame({'points': [11, 17, 16, 18, 22, 25, 26, 24, 29], 'assists': [5, 7, 7, 9, 12, 9, 9, 4, 8]}) #add lines to plot plt.plot(df['points'], label='Points', color='green') plt.plot(df['assists'], label='Assists', color='steelblue') #place legend in center right of plot plt.legend(bbox_to_anchor=(1.02, 1), loc='upper left', borderaxespad=0)
And here’s how to place the legend outside the bottom right corner of the plot:
import pandas as pd import matplotlib.pyplot as plt #create data df = pd.DataFrame({'points': [11, 17, 16, 18, 22, 25, 26, 24, 29], 'assists': [5, 7, 7, 9, 12, 9, 9, 4, 8]}) #add lines to plot plt.plot(df['points'], label='Points', color='green') plt.plot(df['assists'], label='Assists', color='steelblue') #place legend in center right of plot plt.legend(bbox_to_anchor=(1.02, 0.1), loc='upper left', borderaxespad=0)
Refer to the matplotlib documentation for a detailed explanation of the bbox_to_anchor() argument.
Additional Resources
The following tutorials explain how to perform other common operations in Matplotlib:
How to Change Legend Font Size in Matplotlib
How to Add a Title to Legend in Matplotlib
How to Adjust Title Position in Matplotlib
How to Adjust Axis Label Position in Matplotlib