在matplotlib中操纵x轴刻度标签 [英] Manipulating x axis tick labels in matplotlib
问题描述
我注意到,当条形图中的数据条数为5个或更少时,x轴会自动添加额外的刻度: 我想要的是这样的:
有什么方法可以强制matplotlib为第一个图形的每个条形生成一个刻度标签吗?
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 >
I have noticed that when I have 5 or less bars of data in my bar graph the x-axis automatically adds in extra ticks: What I want is something like this:
Is there any way I can force matplotlib to generate just one tick label per bar for the first graph?
The bar
method takes a parameter align
. Set this parameter as align='center'
. align
aligns the bars on the center of the x values we give it, instead of aligning on the left side of the bar (which is the default).
Then use the xticks
method to specify how many ticks on the x-axis and where to place them.
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()
Docs are at http://matplotlib.org/api/pyplot_api.html
这篇关于在matplotlib中操纵x轴刻度标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!