Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add a title to Seaborn Facet Plot

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

like image 747
collarblind Avatar asked Sep 30 '22 22:09

collarblind


People also ask

How do I add a title to my 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.

How do I add a title to Seaborn FacetGrid?

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.

What is FacetGrid in Seaborn?

FacetGrid object takes a dataframe as input and the names of the variables that will form the row, column, or hue dimensions of the grid. The variables should be categorical and the data at each level of the variable will be used for a facet along that axis.

What is Col_wrap?

col_wrapint. “Wrap” the column variable at this width, so that the column facets span multiple rows. Incompatible with a row facet. share{x,y}bool, 'col', or 'row' optional. If true, the facets will share y axes across columns and/or x axes across rows.


2 Answers

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.

like image 244
cphlewis Avatar answered Oct 19 '22 16:10

cphlewis


g.fig.subplots_adjust(top=0.9)
g.fig.suptitle('Title', fontsize=16)

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

like image 45
Taras Avatar answered Oct 19 '22 15:10

Taras