下面是我的源代码:
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()输出如下:

如何像第一行一样标记第一行之后的行?为什么我的嵌套for循环不能工作?
发布于 2018-09-26 03:42:15
如果你查看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')输出:

PS。我曾就读于布克·华盛顿大学(HSEP)。我的HISD学校在哪里?
https://stackoverflow.com/questions/52505002
复制相似问题