How to add a title to a seaborn plot ?

Published: January 21, 2024

Updated: January 21, 2024

Tags: Python; Seaborn;

DMCA.com Protection Status

Introduction

When creating data visualizations with the Python library, Seaborn, it is important to add a title to your plot. A title provides context and information about the overall purpose of your visualization.

In this article, we will explore the process of adding a title to a seaborn plot and delve into various ways to customize it.

Create a seaborn scatter plot

To demonstrate the process of adding a title, let's utilize one of the available datasets in seaborn called "tips". We can acquire this dataset by importing seaborn as sns and using the command:

import seaborn as sns

tips = sns.load_dataset("tips")

print( tips.head() )

The code will show:

   total_bill   tip     sex smoker  day    time  size
0       16.99  1.01  Female     No  Sun  Dinner     2
1       10.34  1.66    Male     No  Sun  Dinner     3
2       21.01  3.50    Male     No  Sun  Dinner     3
3       23.68  3.31    Male     No  Sun  Dinner     2
4       24.59  3.61  Female     No  Sun  Dinner     4

After obtaining the dataset, we can create a simple scatter plot using seaborn with the columns "total_bill" and "tip". The code for this plot would be:

sns.scatterplot(data=tips, x="total_bill", y="tip")

How to add a title to a seaborn plot ?
How to add a title to a seaborn plot ?

Add a title to the plot using set_title()

When using Seaborn to create a box plot, you will receive a Matplotlib axes instance. Unlike pyplot, which utilizes plt.title() method, the equivalent for an axes is ax.set_title():

import seaborn as sns

tips = sns.load_dataset("tips")

sns.scatterplot(data=tips, x="total_bill", y="tip").set_title("How to add a title to a seaborn plot ?")

This will add the title "How to add a title to a seaborn plot ?" to your plot.

How to add a title to a seaborn plot ?
How to add a title to a seaborn plot ?

Personally, I prefer to place the title on a new line, like this:

sp = sns.scatterplot(data=tips, x="total_bill", y="tip")

sp.set_title("How to add a title to a seaborn plot ?")

The outcome will be identical to the one mentioned earlier.

Additional tips

You can also customize the font, size, and placement of the title using additional arguments in the set_title() function. For example:

Changing title color

To change the color of the title, we can use the color parameter in the set_title() function and pass in a color name or a hex code.

sp.set_title("How to add a title to a seaborn plot ?", color='red')

The above code will result in a red title:

How to add a title to a seaborn plot ?
How to add a title to a seaborn plot ?

Changing title position

By default, the title is centered at the top of the seaborn plot. However, we can also change its position using the loc parameter in the plt.title() function.

There are three possible values for the loc parameter:

  • 'center': This is the default value and it centers the title at the top of the plot.
  • 'left': This aligns the title to the left side of the plot.
  • 'right': This aligns the title to the right side of the plot.

Let's see how these different positions affect the title placement in our previous example:

sp.set_title("How to add a title to a seaborn plot ?", color='red', loc='left')

Left-aligned title:

How to add a title to a seaborn plot ?
How to add a title to a seaborn plot ?

Changing title fontsize

By default, the font size of the title in a seaborn plot is 12. However, we can easily change it to suit our preferences. To do so, we can use the title() function along with the fontsize parameter. Let's see an example:

sp.set_title("How to add a title to a seaborn plot ?", color='red', loc='left', fontsize=14)

How to add a title to a seaborn plot ?
How to add a title to a seaborn plot ?

Writing title on multiple lines

To add a multi-line title, we can use the \n character to indicate line breaks within the title. For example:

sp.set_title("How to add a title \n to a seaborn plot ?", color='red', loc='left', fontsize=14)

How to add a title to a seaborn plot ?
How to add a title to a seaborn plot ?

References

Links Site
seaborn.scatterplot seaborn.pydata.org
matplotlib.axes.Axes.set_title matplotlib.org
Image

of