Skip to content

Seaborn Titles and Axis Labels: Add and Customize

Seaborn Titles and Axis Labels Add and Customize Cover Image

In this tutorial, you’ll learn how to add titles and axis labels to Seaborn plots. Seaborn is built on top of Matplotlib, which allows you to add and customize titles in significant detail. Similarly, Seaborn makes adding and customizing titles or axis labels simple and intuitive.

By the end of this tutorial, you’ll have learned the following:

  • How to add and customize titles to Seaborn visualizations
  • How to add and customize x-axis and y-axis labels to Seaborn visualizations
  • How to add and customize titles for FacetGrid (multi-plot) plots in Seaborn

How to Add a Title to a Seaborn Chart

In order to add a title to a Seaborn chart, you can use the .set_title() method. The method allows you to add and customize a title. Under the hood, Seaborn uses Matplotlib, which allows you to customize the titles to a great extent.

Let’s see how we can use the .set_title() method to add a simple title to a chart:

# Adding a Title
import seaborn as sns
import matplotlib.pyplot as plt

df = sns.load_dataset('tips')

sns.barplot(data=df, x='day', y='tip', ci=None).set_title('Sample Title')
plt.show()

This returns the following image:

Adding a Simple Title to a Seaborn Plot
Adding a Simple Title to a Seaborn Plot

By default, Seaborn won’t add very much styling to the title that you add. However, it’s simple to customize the title to the exact style you’re looking for. Let’s take a look at that next.

How to Customize a Title in a Seaborn Chart

In order to customize titles in Seaborn, you can use the fontdict= parameter of the .set_title() method. As the name implies, the method lets you pass in a dictionary that controls the font of the title. Let’s take a look at how we can customize the title font size, font weight, and font color.

Let’s start by looking at how we can customize the font size of a title in a Seaborn chart.

Customize Title Font Size in a Seaborn Chart

In order to customize the title font size we can pass in either the 'fontsize' or 'size' key into the fontdict= parameter.

The font size can either be a specific pixel size or a named font size. Seaborn supports the following font sizes: xx-small, x-small, small, medium, large, x-large, xx-large, larger, smaller, None.

Let’s see how we can customize the title font size in Seaborn with a practical example:

# Customizing Title Font Size
import seaborn as sns
import matplotlib.pyplot as plt
df = sns.load_dataset('tips')

chart = sns.barplot(data=df, x='day', y='tip', ci=None)
chart.set_title('Sample Title', fontdict={'size': 30})
plt.show()

Notice how we’re assigning the plot to a variable chart and then applying the .set_title() method to this object. We could also simply chain the method as we did before. However, this allows us to later customize the axis labels more easily.

This returns the following visualization:

Customizing Title Font Size in Seaborn
Customizing Title Font Size in Seaborn

In the following section, you’ll learn how to customize the title font-weight in a Seaborn chart.

Customize Title Font Weight in a Seaborn Chart

In order to customize the title font weight, you can use the 'fontweight' or 'weight' key in the fondtdict= parameter.

The font weight can be any of the following options: 'normal''bold''heavy''light''ultrabold', and 'ultralight'. By default, as the name implies, Seaborn will use 'normal'.

Let’s see how we can customize a Seaborn title by making it bold:

# Customizing Title Font Weight
import seaborn as sns
import matplotlib.pyplot as plt

df = sns.load_dataset('tips')

chart = sns.barplot(data=df, x='day', y='tip', ci=None)
chart.set_title('Sample Title', fontdict={'size': 30, 'weight': 'bold'})
plt.show()

This returns the following image:

Customizing Title Font Weight in Seaborn
Customizing Title Font Weight in Seaborn

We can see that the text has been bolded. We’re also using the larger font size that we applied earlier.

In the following section, you’ll learn how to customize the title font color in a Seaborn chart.

Customize Title Font Color in a Seaborn Chart

In order to customize the title font weight, you can use the 'color' key in the fondtdict= parameter.

Because Seaborn uses Matplotlib under the hood, you have access to pass in any of the many color options available in Matplotlib. Matplotlib provides a ton of different options of colors: for example, you can use any RGB value (such as (0.1, 0.1, 0.1)) or any HEX value (such as '#0f0f0f').

Similarly, you can use named colors that are provided from Matplotlib 'b' for blue or CSS4 named colors, such as 'aquamarine'.

Let’s see how we can change our font color to 'grey' in our Seaborn title:

# Customizing Title Font Color
import seaborn as sns
import matplotlib.pyplot as plt

df = sns.load_dataset('tips')

chart = sns.barplot(data=df, x='day', y='tip', ci=None)
chart.set_title('Sample Title', fontdict={'size': 30, 'weight': 'bold', 'color': 'grey'})
plt.show()

This returns the following image, where our title has been colored grey:

Customizing Font Title Color in Seaborn
Customizing Font Title Color in Seaborn

In the following section, you’ll learn how about some of the other customization options available in Seaborn title fonts.

Other Customization Options for Title Fonts in Seaborn

Seaborn provides a number of different options for customizing title fonts. For example, you can customize the rotation using the 'rotation' key and the background color using the 'backgroundcolor' key in the fontdict= parameter.

The table below breaks down all of the different options available in Seaborn font title options:

PropertyDescriptionValue Type
alphaThe transparency of your titlefloat
backgroundcolorThe background color of your titleany matplotlib color
bboxThe bounding box of the textRectangle prop dict plus key ‘pad’ which is a pad in points
clip_boxThe clipping box of the titlea matplotlib.transform.Bbox instance
clip_onWhether the artist should use clippingbool
clip_pathThe path to the clippera Path instance and a Transform instance, a Patch
colorThe color of the textany matplotlib color
familyThe font family to use'serif''sans-serif', 'cursive''fantasy', 'monospace'
fontpropertiesDifferent Matplotlib fontpropertiesFontProperties
horizontalalignment or haThe horizontal alignment of the text 'center', 'right', 'left'
labelThe label of the textany string
linespacingThe line spacing to usefloat
multialignmentHow to align your text 'center', 'right', 'left'
name or fontnameThe font namestring e.g., 'Times New Roman' 
pickerThe artist pickerNone, float, boolean
positionThe specific position to place the text(x, y)
rotationThe rotation to apply to the textangle in degrees, 'vertical', 'horizontal'
size or fontsizeThe font size to usesize in points or relative size
style or fontstyleThe font style to use'normal', 'italic', 'oblique'
textThe text to includestring or anything printable with ‘%s’ conversion
transformAny Matplotlib Transform valuesTransform subclass
variantThe variant to apply to your text'normal', 'small-caps'
verticalalignment or vaThe vertical alignment of your text'center''top''bottom''baseline'
visibleWhether the title is visible or notbool
weight or fontweightThe font weight of your text'normal''bold',  'heavy',  'light', 'ultrabold', 'ultralight'
x, yThe x, y position of the textfloat
zorderThe z order of the textany number
The various font attributes to customize Seaborn titles with

In the following section, you’ll learn how to add and customize axis labels in Seaborn charts.

How to Add and Customize Axis Labels in Seaborn Charts

Seaborn also makes it simple to add and customize axis labels. We can add x-axis and y-axis labels using the .set_xlabel() and .set_ylabel() methods, respectively. Because they return Text objects similar to setting titles, we can apply the fontdict= parameter as we did before.

Let’s see how we can add and customize axis labels in Seaborn:

# Adding y-axis and x-axis label
import seaborn as sns
import matplotlib.pyplot as plt

df = sns.load_dataset('tips')
chart = sns.barplot(data=df, x='day', y='tip', ci=None)
chart.set_title('Sample Title', fontdict={'size': 30, 'weight': 'bold'})
chart.set_xlabel('Day of Week', fontdict={'size': 15})
chart.set_ylabel('Tip Amount', fontdict={'size': 15})
plt.show()

This returns the following visualization:

Adding x-axis and y-axis Labels in Seaborn
Adding x-axis and y-axis Labels in Seaborn

We can see that the x-axis and y-axis labels have been modified and customized.

If you’re just looking to add titles and axis labels, you can simply use the .set() method. This allows you to simply pass in strings for the title and axis labels. Let’s take a look at what this looks like:

# Adding Labels in an Easier Way
import seaborn as sns
import matplotlib.pyplot as plt

df = sns.load_dataset('tips')
sns.barplot(data=df, x='day', y='tip', ci=None).set(title='Sample Title', xlabel='Day', ylabel='Tip')
plt.show()

This returns the following image:

A simpler way of adding titles and axis labels in Seaborn
A simpler way of adding titles and axis labels in Seaborn

We can see that it’s an easier way to describe your data but doesn’t provide as much flexibility as the .set_title(), set_xlabel(), and .set_ylabel() methods.

How to Add and Customize Titles for Seaborn FacetGrid Plots

When working with Seaborn FacetGrid plots, adding titles works slightly differently. One of the ways in which to create these plots by passing additional variables into the col= (column) or row= parameters. This creates additional subplots based on the same data.

How to Add a Title to a Seaborn FacetGrid Plot

Let’s see how we can add a title to our FacetGrid plot. In the code below, take note of two lines. By using the .subplots_adjust(), we adjust the spacing to give the title a bit more room. This process is a bit heuristic and involves a little trial and error to find the best spacing.

Additionally, we use the .fig.suptitle() method to add a title to the figure as a whole (rather than to a single plot). Let’s see what this looks like:

# Adding a Title to a FacetGrid Plot
import seaborn as sns
import matplotlib.pyplot as plt

df = sns.load_dataset('tips')
scatter = sns.relplot(data=df, x='total_bill', y='tip', col='sex', row='smoker')

scatter.fig.subplots_adjust(top=0.92)
scatter.fig.suptitle('Tips Broken Out by Gender', size=15)
scatter.set_axis_labels('Total Bill Amount ($)', 'Tip Amount ($)')
plt.show()

This returns the following image:

Adding a Title to a Seaborn FacetGrid Plot
Adding a Title to a Seaborn FacetGrid Plot

In the following section, you’ll learn how to add and customize titles to the subplots in a Seaborn FacetGrid.

How to Customize Titles for Inner Seaborn Plots with Columns and Rows

In order to add titles to subplots in Seaborn, we can use the .set_titles() method. Note that this method references titles, rather than a single title.

The way that the method works is by passing in templates for the columns and rows of subplots that make reference to either the col_name or row_name, which allows you to use the column or row variable.

Let’s take a look at an example of how to add titles to subplots in Seaborn plots:

# Adding Titles to Subplots
import seaborn as sns
import matplotlib.pyplot as plt

df = sns.load_dataset('tips')
scatter = sns.relplot(data=df, x='total_bill', y='tip', col='sex', row='smoker')

scatter.fig.subplots_adjust(top=0.92)
scatter.fig.suptitle('Tips Broken Out by Gender', size=15)
scatter.set_axis_labels('Total Bill Amount ($)', 'Tip Amount ($)')
scatter.set_titles(col_template='{col_name} Visitors', row_template='Smoker: {row_name}', size=12)
plt.show()

In the example above, we joined the column name with the string ' Visitors', which allowed us to change the title to Male Visitors and Female Visitors. Similarly, we prefixed the smoker column with a descriptive label.

Adding Titles to Subplots in Seaborn FacetGrid
Adding Titles to Subplots in Seaborn FacetGrid

In the example above, we were able to add descriptive labels to the subplots in Seaborn. Note that we’re not specifying the exact title, but rather the labels based on the pattern breakdowns.

Frequently Asked Questions

How can you add a title to a Seaborn chart?

In order to add a title to a Seaborn chart, you can use the .set_title() method, which lets you pass in a string.

How can you customize titles in Seaborn charts (such as size, weight, and color)?

In order to customize titles in Seaborn charts, you can pass a dictionary of customizations into the fontdict parameter of the .set_title() method. The dictionary accepts keys such as ‘weight’, ‘size’, and ‘color’.

How can you modify axis labels in Seaborn charts?

To modify axis labels in Seaborn charts, you can use the .set_xlabel() and .set_ylabel() methods. The methods allow you to pass in strings to set the label. You can also customize the labels by passing a dictionary into the fontdict parameter of the method. The dictionary accepts keys such as ‘weight’, ‘size’, and ‘color’.

Conclusion

In this tutorial, you learned how to add and customize titles and axis labels in Seaborn visualizations. Seaborn makes it simple to create beautiful visualizations. Similarly, it’s equally simple to add and customize titles and axis labels. Because Seaborn uses Matplotlib under the hood, you get access to all of the customization options that the library has to offer.

You first learned how to add titles to Seaborn plots and how to customize them by using Matplotlib customizations. Then, you learned how to add and customize axis labels in Seaborn. Finally, you learned how to customize titles in Seaborn subplots in a FacetGrid.

Additional Resources

To learn more about related topics, check out the resources below:

Leave a Reply

Your email address will not be published. Required fields are marked *