Skip to content
Related Articles

Related Articles

How to change Seaborn legends font size, location and color?
  • Last Updated : 17 Feb, 2021

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 matlotlib library.
  2. Using matplotlib.pyplot.legend() function from matlotlib library.
  • Using matplotlib.pyplot.setp() function from matlotlib 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 ilustration
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 matlotlib library:-

This is one of the easiest methods to change the font size of any Seabron 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 ilustration
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 matlotlib 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 ilustration
fg = sns.stripplot(x="sex", y="total_bill"
                   hue="day", data=tips, jitter=True)
  
# to chnage 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 matlotlib 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:

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course

My Personal Notes arrow_drop_up
Recommended Articles
Page :