matplotlib中缺失的标签 - 条形图

6
当我绘制水平条形图时,当我手动设置y轴时,我发现x轴和y轴都缺少一个图例,如下所示。
bar_locations = np.arange(6)
ax.barh(bar_locations, data ,alpha=.5)
bar_locations = np.arange(6)
# data = [55, 22, 40, 56, 109, 180]
# labels = ['others', u'Belts', u'Apparel & Accessories > Jewelry', u'Jewelry', u'Fragrances', u'Watches']
ax.barh(bar_locations, data ,alpha=.5)
ax.set_yticklabels(labels)
fig.tight_layout()

enter image description here

1个回答

6

您需要设置刻度位置

ax.set_yticks(bar_locations)
ax.set_yticklabels(labels)

代码中没有 x 轴标签?!您可以使用 ax.set_xlabel("label") 来设置它。 - ImportanceOfBeingErnest
x轴已经根据数据自动绘制了吧? #data = [55, 22, 40, 56, 109, 180] - Hari Krishnan
对于条形图,它将从0开始,并延伸到轴上显示的最大值的约10%。 - ImportanceOfBeingErnest
但是X轴上的最终图例是150。最大值为180。应该大于190,对吧? - Hari Krishnan
正如所说,这是自动确定的。在这里,因为下一个步骤是50,而轴长度小于200,所以200不会显示为标签。当然,您可以根据自己的喜好设置标签,ax.set_xticks([50,99,137,190]) - ImportanceOfBeingErnest

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接