How to Add a Title to Seaborn Plots?
In this article, we will discuss how to add a title to Seaborn Plots in Python.
Method 1: Adding title using set method
set method takes 1 argument “title” as a parameter which stores Title of a plot.
Syntax
set(title=”Title of a plot”)
Example:
Here, we are going to create a dataframe with months and average temperatures
Python3
import pandas as pd
import seaborn as sns
temperature = pd.DataFrame({ 'Month' : [ 'Jan' , 'Feb' , 'Mar' ,
'Apr' , 'May' , 'Jun' ,
'Jul' , 'Aug' , 'Sep' ,
'Oct' , 'Nov' , 'Dec' ],
'Avg_Temperatures' : [ 22 , 25 , 28 ,
32 , 35 , 44 ,
38 , 32 , 25 ,
23 , 20 , 18 ]})
sns.lineplot(x = 'Month' , y = 'Avg_Temperatures' , data = temperature). set (
title = "Monthly Avg Temperatures" )
|
Output
Method 2: Adding title using suptitle method
suptitle method takes a string which is title of plot as parameter.
Syntax
fig.suptitle(‘Title of plot’)
Example:
Python3
import pandas as pd
import seaborn as sns
temperature = pd.DataFrame({ 'Month' : [ 'Jan' , 'Feb' , 'Mar' ,
'Apr' , 'May' , 'Jun' ,
'Jul' , 'Aug' , 'Sep' ,
'Oct' , 'Nov' , 'Dec' ],
'Avg_Temperatures' : [ 22 , 25 , 28 ,
32 , 35 , 44 ,
38 , 32 , 25 ,
23 , 20 , 18 ]})
plot = sns.relplot(x = 'Month' , y = 'Avg_Temperatures' , data = temperature)
plot.fig.suptitle( 'Monthly Avg Temperatures' )
|
Output
Method 3: Adding title using set_title
set_title method takes a string as parameter which is title of plot.
Syntax
set_title(‘Title of plot’)
Example:
Python3
import pandas as pd
import seaborn as sns
temperature = pd.DataFrame({ 'Month' : [ 'Jan' , 'Feb' , 'Mar' ,
'Apr' , 'May' , 'Jun' ,
'Jul' , 'Aug' , 'Sep' ,
'Oct' , 'Nov' , 'Dec' ],
'Avg_Temperatures' : [ 22 , 25 , 28 ,
32 , 35 , 44 ,
38 , 32 , 25 ,
23 , 20 , 18 ]})
plot = sns.barplot(x = 'Month' , y = 'Avg_Temperatures' ,
data = temperature).set_title( 'Monthly Avg Temperatures' )
|
Output
Last Updated :
19 Dec, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...