在matplotlib中操作x轴刻度标签

2024-04-20 09:42:10 发布

您现在位置:Python中文网/ 问答频道 /正文

我注意到当我的条形图中有5条或更少的数据时,x轴会自动添加额外的刻度: enter image description here 我想要的是这样的东西:

enter image description here

有没有任何方法可以强制matplotlib为第一个图形的每个条生成一个tick标签?


Tags: 数据方法图形matplotlib标签条形图tick刻度
1条回答
网友
1楼 · 发布于 2024-04-20 09:42:10

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()

文件在http://matplotlib.org/api/pyplot_api.html

相关问题 更多 >