【问题标题】:Label Matplotlib subplot y-axes with list of strings用字符串列表标记 Matplotlib 子图 y 轴
【发布时间】:2019-12-05 11:33:24
【问题描述】:

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

但是,当我提醒它我希望 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 标签?

【问题讨论】:

    标签: python string matplotlib label axis


    【解决方案1】:

    似乎您只需要为每个子图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))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-10-22
      • 2020-06-22
      • 1970-01-01
      • 2021-01-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多