在matplotlib中操作x轴刻度标签

Osm*_*hop 6 python matplotlib bar-chart pandas

我注意到,当我的条形图中有5个或更少数据条时,x轴会自动添加额外的刻度: 在此输入图像描述 我想要的是这样的:

在此输入图像描述

有没有什么方法可以强制matplotlib为第一个图形每个条形成一个刻度标签?

twa*_*lig 20

bar方法采用参数align.将此参数设置为align='center'.align对齐我们给出的x值中心的条形,而不是在条形图的左侧对齐(这是默认值).

然后使用该xticks方法指定x轴上有多少刻度以及放置它们的位置.

import matplotlib.pyplot as plot

x = range(1, 7)
y = (0, 300, 300, 290, 320, 315)
plot.bar(x, y, width=0.7, align="center")

ind = range(2, 7)    # the x locations for the groups
plot.xticks(ind, x)

plot.axhline(305, linewidth=3, color='r')

plot.show()
Run Code Online (Sandbox Code Playgroud)

文档位于http://matplotlib.org/api/pyplot_api.html