简体   繁体   English

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

[英]Label Matplotlib subplot y-axes with list of strings

I'm using Matplotlib to create 2 side-by-side horizontal bar charts showing regression coefficient importance across several words.我正在使用 Matplotlib 创建 2 个并排的水平条形图,显示多个单词的回归系数重要性。 I'd like to label the y-axes with each word in the list.我想用列表中的每个单词标记 y 轴。

Every other word is appended to the y-axis when I try this:当我尝试这样做时,所有其他单词都会附加到 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')

所有其他标签都会出现

However, when I remind it that I'd like to have 10 ticks for 10 words ( plt.yticks(np.arange(0,10)) ), it fixes the second subplot:但是,当我提醒它我想要 10 个单词( plt.yticks(np.arange(0,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')

现在第二个子图具有正确的 y-tick 标签

How do I get both subplots to have the proper y-tick labels?如何让两个子图都具有正确的 y-tick 标签?

Seems like you just need to set_yticks for each subplot.似乎您只需要为每个子图设置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))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM