How can I change the legend font size in a Seaborn plot?

The process of changing the legend font size in a Seaborn plot involves adjusting the font size parameter within the legend function. This can be achieved by first identifying the legend object in the plot and then using the set_fontsize() method to specify the desired font size. This allows for customization of the legend font size to better suit the visual needs of the plot.

Change Legend Font Size in a Seaborn Plot


You can use the following syntax to change the font size within a legend of a seaborn plot:

plt.legend(title='Team', fontsize='10', title_fontsize='14')

The fontsize argument specifies the font size for the labels in the legend and the title_fontsize specifies the font size for the title of the legend.

The following example shows how to use this function in practice.

Example: Changing Legend Font Size in a Seaborn Plot

The following code shows how to create a scatterplot in Seaborn and specify the font size for both the labels and the title within the legend:

import pandas as pd
import seaborn as sns
import matplotlib.pyplotas plt
sns.set_style('whitegrid')

#create data
df = pd.DataFrame({'points': [25, 12, 15, 14, 19, 23, 25, 29],
                   'assists': [5, 7, 7, 9, 12, 9, 9, 4],
                   'team': ['A', 'A', 'A', 'A', 'B', 'B', 'B', 'B']})

#create scatterplot
sns.scatterplot(data=df, x='points', y='assists', hue='team')

#add legend
plt.legend(title='Team', fontsize='10', title_fontsize='14')

The font size arguments can also take on the following values:

  • xx-small
  • x-small
  • small
  • medium
  • large
  • x-large
  • xx-large

The following example shows how to use these arguments in practice:

import pandas as pd
import seaborn as sns
import matplotlib.pyplotas plt
sns.set_style('whitegrid')

#create fake data
df = pd.DataFrame({'points': [25, 12, 15, 14, 19, 23, 25, 29],
                   'assists': [5, 7, 7, 9, 12, 9, 9, 4],
                   'team': ['A', 'A', 'A', 'A', 'B', 'B', 'B', 'B']})

#create scatterplot
sns.scatterplot(data=df, x='points', y='assists', hue='team')

#add legend
plt.legend(title='Team', fontsize='medium', title_fontsize='x-large')

Reference the for an in-depth explanation of the plt.legend() function.

Additional Resources

The following tutorials explain how to perform other common tasks in seaborn:

How to Place Legend Outside a Seaborn Plot

x