用字符串列表标记Matplotlib子图y轴

我正在使用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')

用字符串列表标记Matplotlib子图y轴

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

# plot word importance bar graphs
fig,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)),highrev_topten['Coefficient'])
axes[1].set_yticklabels(list(highrev_topten['Word']))
axes[1].set_xlabel('Coefficient')

用字符串列表标记Matplotlib子图y轴

我如何两个子图都具有正确的y-tick标签?

maochanggeng 回答:用字符串列表标记Matplotlib子图y轴

似乎您只需要为每个子图set_yticks

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))
本文链接:https://www.f2er.com/2973320.html

大家都在问