在matplotlib中操纵x轴刻度标签 [英] Manipulating x axis tick labels in matplotlib

查看:592
本文介绍了在matplotlib中操纵x轴刻度标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我注意到,当条形图中的数据条数为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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆