将文本添加到seaborn中的每个子图

26

我正在使用seaborn制作条形图,并希望在每个子图中添加一些文本。我知道如何向整个图形添加文本,但我想访问每个子图并添加文本。我正在使用以下代码:

import seaborn as sns
import pandas as pd

sns.set_style("whitegrid")

col_order=['Deltaic Plains','Hummock and Swale', 'Sand Dunes']

g = sns.FacetGrid(final, col="Landform", col_wrap=3,despine=False, sharex=False,col_order=col_order)

g = g.map(sns.barplot, 'Feature', 'Importance')

[plt.setp(ax.get_xticklabels(), rotation=45) for ax in g.axes.flat]

for ax, title in zip(g.axes.flat, col_order):
    ax.set_title(title)

g.fig.text(0.85, 0.85,'Text Here', fontsize=9) #add text

这给了我这个: 输入图片说明
2个回答

28

在你的for循环中,每个小图都可以通过ax来访问。

for ax, title in zip(g.axes.flat, col_order):
    ax.set_title(title)
    ax.text(0.85, 0.85,'Text Here', fontsize=9) #add text

0

由于图形对象定义了axes属性以访问其上定义的Axes对象,因此通过在FacetGrid上定义的Figure对象访问子图的另一种方式也是可行的。

for ax in g.fig.axes:
    ax.text(0.85, 0.85,'Text Here', fontsize=9)

如果FacetGrid是当前的图形实例,那么以下方法也可以使用。
for ax in plt.gcf().axes:
    ax.text(0.85, 1.85,'Text Here', fontsize=9)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接