带有字符串列表的Label Matplotlib子图y轴

问题描述 投票:0回答:1

我正在使用Matplotlib来创建2个并排的水平条形图,这些水平条形图显示了跨多个单词的回归系数的重要性。我想用列表中的每个单词标记y轴。

尝试此操作时,每个其他字词都会附加到y轴上:

# plot word importance bar graphs
fig, axes = plt.subplots(1,2,figsize=(5,10))
plt.subplots_adjust(wspace = 1)

axes[0].set_title('Low revenue')
axes[0].invert_yaxis()
axes[0].barh(np.arange(len(lowrev_topten)), lowrev_topten['Coefficient'])
axes[0].set_yticklabels(list(lowrev_topten['Word']))
axes[0].set_xlabel('Coefficient')

axes[1].set_title('High revenue')
axes[1].invert_yaxis()
axes[1].barh(np.arange(len(highrev_topten)), highrev_topten['Coefficient'])
axes[1].set_yticklabels(list(highrev_topten['Word']))
axes[1].set_xlabel('Coefficient')

Every other label shows up

但是,当我想起我想对10个单词用10个滴答声(plt.yticks(np.arange(0,10)))时,它修复了第二个子图:

# plot word importance bar graphs
fig, axes = plt.subplots(1,2,figsize=(5,10))
plt.subplots_adjust(wspace = 1)
plt.yticks(np.arange(0,10))

axes[0].set_title('Low revenue')
axes[0].invert_yaxis()
axes[0].barh(np.arange(len(lowrev_topten)), lowrev_topten['Coefficient'])
axes[0].set_yticklabels(list(lowrev_topten['Word']))
axes[0].set_xlabel('Coefficient')

axes[1].set_title('High revenue')
axes[1].invert_yaxis()
axes[1].barh(np.arange(len(highrev_topten)), highrev_topten['Coefficient'])
axes[1].set_yticklabels(list(highrev_topten['Word']))
axes[1].set_xlabel('Coefficient')

Now the second subplot has the proper y-tick labels

如何获得两个子图来具有正确的y-tick标签?

python string matplotlib label axis
1个回答
0
投票
fig, axes = plt.subplots(1,2,figsize=(5,10)) ... axes[0].set_yticks(np.arange(0,10)) axes[1].set_yticks(np.arange(0,10))
© www.soinside.com 2019 - 2024. All rights reserved.