我试图使用条形图在y轴上绘制日期时间并在x轴上绘制时间。我需要用y轴的日期时间来指定高度,我不知道该怎么做。
import matplotlib.pyplot as plt
import matplotlib as mpl
import numpy as np
import datetime as dt
# Make a series of events 1 day apart
y = mpl.dates.drange(dt.datetime(2009,10,1),
dt.datetime(2010,1,15),
dt.timedelta(days=1))
# Vary the datetimes so that they occur at random times
# Remember, 1.0 is equivalent to 1 day in this case...
y += np.random.random(x.size)
# We can extract the time by using a modulo 1, and adding an arbitrary base date
times = y % 1 + int(y[0]) # (The int is so the y-axis starts at midnight...)
# I'm just plotting points here, but you could just as easily use a bar.
fig = plt.figure()
ax = fig.add_subplot(111)
ax.bar(times, y, width = 10)
ax.yaxis_date()
fig.autofmt_ydate()
plt.show()
答案 0 :(得分:0)
我认为这可能是你的y系列。 当我执行你的代码时,我会得到一个y轴的图,范围从2011年12月11日到2011年12月21日。
但是,您已经生成了从2009年1月10日到2010年1月15日的日期。
当我调整y限制以考虑到这一点时,它工作正常。我在ax.bar
之后添加了这个。
plt.ylim( (min(y)-1,max(y)+1) )
输出混乱的另一个原因是,由于您选择的宽度为10,因此条形太宽而实际上相互遮挡。
尝试使用ax.plot(times,y,'ro')
查看我的意思。
我使用ax.bar(times,y,width=.1,alpha=.2)
和ax.plot(times,y,'ro')
制作了以下情节,以向您展示我对彼此重叠的条形码的意义:
这些条的宽度为.1,因此如果它们的宽度为10,则它们会完全相互遮挡。