How can I change the position of a legend in Matplotlib? 2

How can I change the position of a legend in Matplotlib?

Matplotlib is a popular Python library used for creating high-quality visualizations. One of the common tasks when creating charts and graphs is to adjust the position of the legend, which is a visual representation of the labels and colors used in the plot. In order to change the position of a legend in Matplotlib, you can use the ‘loc’ parameter and provide a specific location code such as ‘upper left’, ‘lower right’, or ‘center’. Alternatively, you can also use the ‘bbox_to_anchor’ parameter to specify the exact coordinates of the legend. By utilizing these parameters, users can easily customize the position of the legend to best fit their visualization and improve its overall appearance.

Change the Position of a Legend in Matplotlib


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.pyplotas 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.pyplotas 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

For example, here’s how to place the legend outside the top right corner of the plot:

import pandas as pd
import matplotlib.pyplotas 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.pyplotas 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 for a detailed explanation of the bbox_to_anchor() argument.

How to Add a Title to Legend in Matplotlib

Cite this article

stats writer (2024). How can I change the position of a legend in Matplotlib?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-change-the-position-of-a-legend-in-matplotlib/

stats writer. "How can I change the position of a legend in Matplotlib?." PSYCHOLOGICAL SCALES, 13 May. 2024, https://scales.arabpsychology.com/stats/how-can-i-change-the-position-of-a-legend-in-matplotlib/.

stats writer. "How can I change the position of a legend in Matplotlib?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-change-the-position-of-a-legend-in-matplotlib/.

stats writer (2024) 'How can I change the position of a legend in Matplotlib?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-change-the-position-of-a-legend-in-matplotlib/.

[1] stats writer, "How can I change the position of a legend in Matplotlib?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, May, 2024.

stats writer. How can I change the position of a legend in Matplotlib?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.

Download Post (.PDF)
Slide Up
x
PDF
Scroll to Top