如何为 Seaborn Facet Plot 添加标题 [英] How to add a title to Seaborn Facet Plot
问题描述
如何为这个 Seaborne 情节添加标题?让我们给它一个标题我是一个头衔".
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")
推荐答案
稍微更新,使用 seaborn 0.11.1:
Updating slightly, with seaborn 0.11.1:
Seaborn 的 relplot
函数创建一个 FacetGrid 并为每个子图提供自己的解释性标题:
Seaborn's relplot
function creates a FacetGrid and gives each subplot its own explanatory title:
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')
如果直接创建 FacetGrid,就像在原始示例中一样,您将获得列和行标签,而不是单独的子图标签:
If you create the FacetGrid directly, as in the original example, you get column and row labels instead of individual subplot labels:
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!')
FacetGrid 对象是使用 matplotlib Figure 对象构建的,因此我们可以使用通常从 matplotlib 中熟悉的 subplots_adjust
、suptitle
.
The FacetGrid objects are built with matplotlib Figure objects, so we can use subplots_adjust
, suptitle
that may be familiar from matplotlib in general.
这篇关于如何为 Seaborn Facet Plot 添加标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!