How to add a title to Seaborn Facet Plot
Asked Answered
S

7

148

How do I add a title to this Seaborne plot? Let's give it a title 'I AM A TITLE'.

tips = sns.load_dataset("tips")
g = sns.FacetGrid(tips, col="sex", row="smoker", margin_titles=True)
g.map(sns.plt.scatter, "total_bill", "tip")

plot

Sauveur answered 23/4, 2015 at 4:28 Comment(0)
Q
270

Updating slightly, with seaborn 0.11.1:

Seaborn's relplot function creates a FacetGrid and gives each subplot its own explanatory title. You can add a title over the whole thing:

import seaborn as sns
tips = sns.load_dataset('tips')

rp = sns.relplot(data=tips, x='total_bill', y='tip',
                 col='sex', row='smoker',
                 kind='scatter')
# rp is a FacetGrid; 
# relplot is a nice organized way to use it

rp.fig.subplots_adjust(top=0.9) # adjust the Figure in rp
rp.fig.suptitle('ONE TITLE FOR ALL')

Two-by-two grid of scatterplots. Each sub-plot has a title; the grid has a suptitle over all

If you create the FacetGrid directly, as in the original example, it automatically adds column and row labels instead of individual subplot titles. We can still add a title to the whole thing:

from matplotlib.pyplot import scatter as plt_scatter
g = sns.FacetGrid(tips, col='sex', row='smoker', 
                  margin_titles=True)
g.map(plt_scatter, 'total_bill', 'tip')
g.fig.subplots_adjust(top=0.9)
g.fig.suptitle('TITLE!')

Two-by-two grid of scatterplots. Each column has a title and the overall grid has a supertitle.

The FacetGrid objects are built with matplotlib Figure objects, so we can use subplots_adjust, suptitle that may be familiar from matplotlib in general.

Quitt answered 23/4, 2015 at 5:15 Comment(4)
This worked for me but I had to use plt.subplots_adjust(top=0.8) instead of top=0.9.Halinahalite
suptitle also has a y parameter. This worked for me: g.fig.suptitle('foo', y=1.05)Allophone
This does not work if you use col_wrap as then both the figure and the last axes subplot (bottom right) will inherit the same title.Dedication
instead of fiddling with the margins with subplots_adjust() simply calling plt.tight_layout() often times does the job quite nicely for you :)Keikokeil
B
48
g.fig.subplots_adjust(top=0.9)
g.fig.suptitle('Title', fontsize=16)

More info here: http://matplotlib.org/api/figure_api.html

Budd answered 3/2, 2017 at 17:50 Comment(0)
C
22

In ipython notebook, this worked for me!

sns.plt.title('YOUR TITLE HERE')
Counterblow answered 30/9, 2015 at 21:50 Comment(5)
I tried this and it added a title to the bottom right figure, not the whole figureOrsay
Accessing plt via the sns package has been deprecated in 0.8.1. This should be done using plt.title('YOUR TITLE HERE')Halinahalite
AttributeError: module 'seaborn' has no attribute 'plt'Dedication
AttributeError: module 'seaborn' has no attribute 'plt'Bitty
@Dedication & @seralouk: import matplotlib.pyplot as pltBummer
E
2

What worked for me was:

sns.plt.suptitle('YOUR TITLE HERE')

Epiblast answered 13/12, 2016 at 16:17 Comment(2)
AttributeError: module 'seaborn' has no attribute 'plt'Dedication
While this can work if seaborn exposes it's matplotlib import, it's very bad practice to rely on another package to import your dependencies for you. You should just add import matplotlib.pyplot as plt in your code and use plt.suptitle directly.Halinahalite
O
0

The answers using sns.plt.title() and sns.plt.suptitle() don't work anymore.

Instead, you need to use matplotlib's title() function:

import matplotlib.pyplot as plt
sns.FacetGrid(<whatever>)
plt.title("A title")
Orsay answered 19/6, 2018 at 23:57 Comment(3)
This doesn't set the title in the correct location for me.Spinozism
Did you try plt.suptitle()?Orsay
Your answer does not work as it affects the title of the last axes subplot (bottom right). Your suggestion plt.suptitle() also modifies the last axes as well as the main figure.Dedication
M
0
plt.suptitle("Title") 

or

plt.title("Title")

This worked for me.

Meditate answered 26/4, 2020 at 0:45 Comment(1)
The second solution doesn't work, while the first solution creates an ugly interlay of suptitle and facet titles. You need to use using coordinates.Middelburg
N
0

The title will not be center aligned with the subplot titles. To set the position of the title you can use plt.suptitle("Title", x=center)

In my case, my subplots were in a 2x1 grid, so I was able to use bbox = g.axes[0,0].get_position() to find the bounding box and then center=0.5*(bbox.x1+bbox.x2)

Nunnery answered 26/6, 2020 at 14:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.