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

27 Oct, 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.
To change the font size of Seaborn legends there are two different ways which are listed as follows:
Using matplotlib.pyplot.setp() function from matplotlib library.Using matplotlib.pyplot.legend() function from matplotlib library.
Using matplotlib.pyplot.setp() function from matplotlib library.
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 modulesimport seaborn as snsimport matplotlib.pylab as pltsns.set_style("whitegrid") # load datasettips = sns.load_dataset("tips") # depict illustrationgfg = sns.stripplot(x="sex", y="total_bill",                    hue="day", data=tips, jitter=True) # for legend textplt.setp(gfg.get_legend().get_texts(), fontsize='10')  # for legend titleplt.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 modulesimport seaborn as snsimport matplotlib.pylab as pltsns.set_style("whitegrid") # load datasettips = sns.load_dataset("tips") # depict illustrationgfg = sns.stripplot(x="sex", y="total_bill",                    hue="day", data=tips, jitter=True)gfg.legend(fontsize=5)plt.show()
Output:
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 modulesimport seaborn as snsimport matplotlib.pylab as pltsns.set_style("whitegrid") # load datasettips = sns.load_dataset("tips") # depict illustrationfg = sns.stripplot(x="sex", y="total_bill",                   hue="day", data=tips, jitter=True) # to change the legends locationgfg.legend(bbox_to_anchor= (1.2,1))plt.show()
  
Output:
 
 
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 modulesimport matplotlib.pyplot as pltimport numpy as np # depict illustrationg = np.random.rand(20,1)plt.plot(g, label='gfg')legend = plt.legend()frame = legend.get_frame()frame.set_facecolor('green')plt.show()
Output:
gabaa406
simmytarika5
sagar0719kumar
Picked
Python-Seaborn
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Python Dictionary
Enumerate() in Python
How to Install PIP on Windows ?
Iterate over a list in Python
Different ways to create Pandas Dataframe
Python String | replace()
Python program to convert a list to string
sum() function in Python
Create a Pandas DataFrame from Lists
How to drop one or multiple columns in Pandas Dataframe