Manipulating x axis tick labels in matplotlib

假装没事ソ 提交于 2019-12-12 10:47:09

问题


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?


回答1:


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



来源:https://stackoverflow.com/questions/15777945/manipulating-x-axis-tick-labels-in-matplotlib

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!