How to Add Title to Seaborn Plot

Manav Narula Feb 02, 2024
  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
How to Add Title to 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

Author: Manav Narula
Manav Narula avatar Manav Narula avatar

Manav is a IT Professional who has a lot of experience as a core developer in many live projects. He is an avid learner who enjoys learning new things and sharing his findings whenever possible.

LinkedIn