Open In App

How to change Seaborn legends font size, location and color?

Last Updated : 27 Oct, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

Seaborn is a library for making statistical graphics on top of matplotlib with pandas data structures in python. Seaborn legend is the dialog box which is located on the graph which includes the description of the different attributes with their respected colors in the graph. We can easily change the properties of the seaborn legend including font size, location, background colour, and many others.

Here, We will learn about the way to change the font size, location, and the color of seaborn legend.

Changing the font size of Seaborn legends

To change the font size of Seaborn legends there are two different ways which are listed as follows:

  1. Using matplotlib.pyplot.setp() function from matplotlib library.
  2. Using matplotlib.pyplot.legend() function from matplotlib library.
  • Using matplotlib.pyplot.setp() function from matplotlib library:

With the help of this method, the user can easily change the font size of the seaborn legends by specifying the particular font size, graph, and subject(whether the user has to change the font size of the text of the title in the legends.

Python3




# import modules
import seaborn as sns
import matplotlib.pylab as plt
sns.set_style("whitegrid")
 
# load dataset
tips = sns.load_dataset("tips")
 
# depict illustration
gfg = sns.stripplot(x="sex", y="total_bill",
                    hue="day", data=tips, jitter=True)
 
# for legend text
plt.setp(gfg.get_legend().get_texts(), fontsize='10'
 
# for legend title
plt.setp(gfg.get_legend().get_title(), fontsize='20'
plt.show()


Output:

  • Using matplotlib.pyplot.legend() function from matplotlib library:-

This is one of the easiest methods to change the font size of any Seaborn legends, in this we just have to pass the parameter of the fontsize which allows us to pass the font-size value and it will change the font size.

Python3




# import modules
import seaborn as sns
import matplotlib.pylab as plt
sns.set_style("whitegrid")
 
# load dataset
tips = sns.load_dataset("tips")
 
# depict illustration
gfg = sns.stripplot(x="sex", y="total_bill",
                    hue="day", data=tips, jitter=True)
gfg.legend(fontsize=5)
plt.show()


Output:

Changing the location of Seaborn legends

We use matplotlib.pyplot.legend() function from matplotlib library and pass the bbox_to_anchor parameter which allows us to pass an (x,y) tuple with the required offset for changing the location of the seaborn legends.

Python3




# import modules
import seaborn as sns
import matplotlib.pylab as plt
sns.set_style("whitegrid")
 
# load dataset
tips = sns.load_dataset("tips")
 
# depict illustration
fg = sns.stripplot(x="sex", y="total_bill",
                   hue="day", data=tips, jitter=True)
 
# to change the legends location
gfg.legend(bbox_to_anchor= (1.2,1))
plt.show()


 
 

Output:

 

Changing the color of Seaborn legends

 

Just with the use of matplotlib.pyplot.set_facecolor() function from matplotlib library and pass the name of the color user want to in the seaborn legends.

 

Python3




# import modules
import matplotlib.pyplot as plt
import numpy as np
 
# depict illustration
g = np.random.rand(20,1)
plt.plot(g, label='gfg')
legend = plt.legend()
frame = legend.get_frame()
frame.set_facecolor('green')
plt.show()


Output:



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads