Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
298 views
in Technique[技术] by (71.8m points)

python - 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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...