How to add a title to a Seaborn plot

Seaborn is a popular Python visualization library built on top of matplotlib that provides a high-level interface for drawing attractive and informative statistical graphics. These plots require titles and labels (on the x and y axes) to present data vividly. Moreover, since Seaborn is built on top of the matplotlib.pyplot library, it can easily be linked with commonly used libraries (e.g., pandas).

Adding labels and a title to a box plot

In seaborn, we use the x and y parameters of the sns.boxplot() function to label the x and y-axis and the .set() to add a title to the plot.

Example

Let’s create a box plot and label it as follows:

import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
df = pd.DataFrame({'Score': [33, 24, 29, 25, 30, 29, 34, 37],
'Player': ['X', 'X', 'X', 'X', 'Y', 'Y', 'Y', 'Y']})
sns.boxplot(data=df, x='Player', y='Score').set(title='Points by Player')

Explanation

  • Lines 4–5: We create a sample DataFrame, df.
  • Line 6: We use the sns.boxplot() function to create a boxplot and set the labels and the .set() attribute to add a title.

Here, seaborn stores the boxplot as a list and then uses its show() function to print it.

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved