gpt4 book ai didi

python - 如何为 Seaborn Facet Plot 添加标题

转载 作者:行者123 更新时间:2023-11-28 19:00:36 27 4
gpt4 key购买 nike

如何为这个 Seaborne 图添加标题?让我们给它起一个标题“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

最佳答案

稍微更新,seaborn 0.11.1:

Seaborn 的 relplot 函数创建了一个 FacetGrid 并为每个子图提供了自己的解释性标题。您可以在整个内容上添加标题:

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

如果您直接创建 FacetGrid,就像在原始示例中一样,它会自动添加列和行标签,而不是单独的子图标题。我们仍然可以为整个事物添加标题:

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.

FacetGrid 对象是用 matplotlib Figure 对象构建的,因此我们可以使用 subplots_adjustsuptitle,这些在 matplotlib 中通常很熟悉。

关于python - 如何为 Seaborn Facet Plot 添加标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53031533/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com