Add Title to Seaborn Plot

  1. Use the set_title() Function to Add a Title to a Seaborn Plot
  2. Use the set() Function to Add a Title to a Seaborn Plot
  3. Use the title() Function to Add a Title to a Seaborn Plot

In this tutorial, we will discuss how to add a title to a seaborn plot.

Use the set_title() Function to Add a Title to a Seaborn Plot

A seaborn plot returns a matplotlib axes instance type object. For such objects, we can use the set_title() function to add a title to the plot.

For example,

import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns

df = pd.DataFrame({"Day 1": [7,1,5,6,3,10,5,8],
                 "Day 2" : [1,2,8,4,3,9,5,2]})

p = sns.lineplot(data = df)
p.set_title("Title")

seaborn title 1

We can also control the size of the title using the fontsize parameter.

Use the set() Function to Add a Title to a Seaborn Plot

The set() function is used to add different elements to the plot and can be used to add the title. We use the title parameter and give it the desired name.

For example,

import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns

df = pd.DataFrame({"Day 1": [7,1,5,6,3,10,5,8],
                 "Day 2" : [1,2,8,4,3,9,5,2]})

p = sns.lineplot(data = df)
p.set(title = "Title")

seaborn title 2

Note that it works similar to the set_title() function discussed earlier.

Use the title() Function to Add a Title to a Seaborn Plot

The matplotlib.pyplot.title() function is used to add a title to the current plot. It offers a lot of customizations via different arguments. A few of them are the loc argument which is used to specify the location; the fontdict() argument controlling the appearance and alignment of the font; and the color argument specifying its color.

In the following code, we add a title to a seaborn plot using this function.

import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns

df = pd.DataFrame({"Day 1": [7,1,5,6,3,10,5,8],
                 "Day 2" : [1,2,8,4,3,9,5,2]})

p = sns.lineplot(data = df)
plt.title('Title')

seaborn title 3

Contribute
DelftStack is a collective effort contributed by software geeks like you. If you like the article and would like to contribute to DelftStack by writing paid articles, you can check the write for us page.