简体   繁体   English

如何为 Seaborn Facet Plot 添加标题

[英]How to add a title to Seaborn Facet Plot

How do I add a title to this Seaborne plot?如何为这个 Seaborne 情节添加标题? 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")

阴谋

Updating slightly, with seaborn 0.11.1:用 seaborn 0.11.1 稍微更新:

Seaborn's relplot function creates a FacetGrid and gives each subplot its own explanatory title. Seaborn 的relplot函数创建一个 FacetGrid 并为每个子图提供自己的解释性标题。 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')

2×2 的散点图网格。每个子情节都有一个标题;网格在所有方面都有一个标题

If you create the FacetGrid directly, as in the original example, it automatically adds column and row labels instead of individual subplot titles.如果直接创建 FacetGrid,就像在原始示例中一样,它会自动添加列和行标签,而不是单独的子图标题。 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!')

2×2 的散点图网格。每列都有一个标题,整个网格都有一个超级标题。

The FacetGrid objects are built with matplotlib Figure objects, so we can use subplots_adjust , suptitle that may be familiar from matplotlib in general.该FacetGrid对象都建有matplotlib图对象,所以我们可以使用subplots_adjustsuptitle ,可能是一般从matplotlib熟悉。

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

More info here: http://matplotlib.org/api/figure_api.html更多信息: http : //matplotlib.org/api/figure_api.html

在 ipython notebook 中,这对我有用!

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

对我有用的是:

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

The answers using sns.plt.title() and sns.plt.suptitle() don't work anymore.使用sns.plt.title()sns.plt.suptitle()的答案不再起作用。

Instead, you need to use matplotlib's title() function:相反,您需要使用 matplotlib 的title()函数:

import matplotlib.pyplot as plt
sns.FacetGrid(<whatever>)
plt.title("A title")
plt.suptitle("Title") 

or或者

plt.title("Title")

This worked for me.这对我有用。

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)要设置标题的位置,您可以使用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)就我而言,我的子图位于 2x1 网格中,因此我能够使用bbox = g.axes[0,0].get_position()找到边界框,然后center=0.5*(bbox.x1+bbox.x2)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM