无法标记多行 sns.catplot()

eka*_*ike 3 python matplotlib seaborn

这是我的源代码:

plot = sns.catplot(x='Year',
                   y='Graduation Rate',
                   col='Group',
                   hue='Campus',
                   kind='bar',
                   col_wrap=4,
                   data=mbk_grad.sort_values(['Group', 'Campus']))

for i in np.arange(2):
    for j in np.arange(4):
        ax = plot.facet_axis(i,j) 
        for p in ax.patches:
            if str(p.get_height()) != 'nan':
                ax.text(p.get_x() + 0.06, p.get_height() * .8, '{0:.2f}%'.format(p.get_height()), color='white', rotation='vertical', size='large')

plt.show()
Run Code Online (Sandbox Code Playgroud)

输出如下:

在此处输入图片说明

如何在第一个标记为第一行之后获取行?为什么我的嵌套 for 循环不起作用?

Sco*_*ton 5

如果您查看plot.axes.shape,您将看到轴数组不是您预期的 (2,4),而是 (8,) 一维数组。这是因为您使用的是col_wrap,而不是定义布局网格。

plot = sns.catplot(x='Year',
                   y='Graduation Rate',
                   col='Group',
                   hue='Campus',
                   kind='bar',
                   col_wrap=4,
                   data=df_sum.sort_values(['Group', 'Campus']))

for i in np.arange(8):
#     for j in np.arange(4):
        ax1 = plot.facet_axis(0,i)
        for p in ax1.patches:
            if str(p.get_height()) != 'nan':
                ax1.text(p.get_x() + 0.06, p.get_height() * .8, '{0:.2f}%'.format(p.get_height()), color='white', rotation='vertical', size='large')
Run Code Online (Sandbox Code Playgroud)

输出:

在此处输入图片说明

附注。我参加了 Booker T. Washington (HSEP)。我的 HISD 学校在哪里?