日期时间轴间距 [英] Datetime axis spacing

查看:274
本文介绍了日期时间轴间距的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个错误栏图,其中xaxis是datetime对象的列表。标准绘图方法将放置第一个和最后一个点,使其正好在图的边界框上。我想要偏移一半,以便清楚地看到第一点和最后一点。

I have an errorbar plot where the xaxis is a list of datetime objects. The standard plotting method will put the first and last point so that they are right on the bounding box of the plot. I would like to offset by a half tick so that the first and last point can be seen clearly.

ax.axis(xmin=-0.5,xmax=len(dates)-0.5)

由于明显的原因不起作用。在没有硬编码任何日期的情况下,可以做到这一点是件好事。

does not work for obvious reasons. It would be nice to be able to do this without hardcoding any dates.

以下内容将会产生一个十分,但只能看到8的情节。 p>

The following will produce a plot which has ten points but you can really only see 8.

import datetime
import matplotlib.pyplot as plt

dates = [datetime.date(2002, 3, 11) - datetime.timedelta(days=x) for x in range(0, 10)]
yvalues = [2, 4, 1,7,9,2, 4, 1,7,9]
errorvalues = [0.4, 0.1, 0.3,0.4, 0.1,.4, 0.1, 0.3,0.4, 0.1]

fig = plt.figure() 
ax = fig.add_subplot(1, 1, 1)
ax.errorbar(dates,yvalues,yerr=errorvalues,fmt='.') 
fig.autofmt_xdate()

plt.show()


推荐答案

可以使用边距设置间距)

You can set spacing with margins()

import datetime
import matplotlib.pyplot as plt

dates = [datetime.date(2002, 3, 11) - datetime.timedelta(days=x) for x in range(0, 10)]
yvalues = [2, 4, 1,7,9,2, 4, 1,7,9]
errorvalues = [0.4, 0.1, 0.3,0.4, 0.1,.4, 0.1, 0.3,0.4, 0.1]

fig = plt.figure() 
ax = fig.add_subplot(1, 1, 1)
ax.errorbar(dates,yvalues,yerr=errorvalues,fmt='.') 
ax.margins(x=0.05)
fig.autofmt_xdate()

plt.show()

这篇关于日期时间轴间距的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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