我有多个数据源在特定时间戳上报告真/假值,如下所示:
{1338: [
(1377259958, False),
(1378703557, True)],
1343: [
(1377259911, True),
(1377812511, False),
(1377814321, True)],
1354: [
(1377260040, False),
(1377296033, True),
(1377382446, False),
(1377566041, True),
(1377582236, False),
(1377638031, True),
(1377641637, False),
(1377652434, True),
(1377814443, False),
(1377987234, True),
(1378073645, False),
(1378160039, True),
(1378246440, False),
(1378257238, True),
(1378341839, False),
(1378421045, True),
(1378514636, False),
(1378613637, True)],
1431: [
(1377260039, False),
(1377729842, True),
(1377731646, False),
(1378703641, True)]
}
现在我想在一个图中绘制这些数据,以便每个数据源都在y轴上,时间在x轴上。每个数据源都应该对数据从True变为False的时间段进行着色。
目前我有这个:
在Reality中有更多的时间戳和更多的数据源,但它始终是相同的过程。
我正在使用带有以下脚本的matplotlib:
def timelines(y, xstart, xstop, ok):
color = 'r' if ok else 'g'
print 'Graph', y, xstart, xstop, color
plt.hlines(y,xstart,xstop,color,lw=3)
plt.vlines(xstart, y+0.45,y-0.45,color,lw=0.5)
plt.vlines(xstop, y+0.45,y-0.45,color,lw=0.5)
maxtime = 0
mintime = time()
for probe, tl in timeline.iteritems():
newmax = max(tl, key=itemgetter(0))[0]
newmin = min(tl, key=itemgetter(0))[0]
if newmax > maxtime:
print "New maximum time: %s (Old: %s)" % (newmax, maxtime)
maxtime = newmax
if newmin < mintime:
print "New minimum time: %s (Old: %s)" % (newmin, mintime)
mintime = newmin
maxprobe = 0
probelist = []
for probe, tl in timeline.iteritems():
print probe, tl
maxprobe += 1
probelist.append(probe)
first = True
startpoint = mintime
for ts in tl:
if ts[0] <= mintime:
first = False
continue
print maxprobe, startpoint, ts[0], ts[1]
if first:
first = False
timelines(maxprobe, int(startpoint), int(ts[0]), not ts[1])
else:
timelines(maxprobe, int(startpoint), int(ts[0]), ts[1])
startpoint=ts[0]
if startpoint < maxtime:
print maxprobe, 'End', startpoint, maxtime, ts[1]
timelines(maxprobe, int(startpoint), maxtime, not ts[1])
label = ['']
label += probelist
plt.ylim((0, maxprobe+1))
plt.yticks(np.arange(0,maxprobe+1), label)
fig = plt.gcf()
fig.set_size_inches(18.5,0.15*maxprobe+1)
plt.savefig('timeline.png')
我没想到的是如何在xaxis中显示格式化的日期而不是时间戳。另外,还有另一种缩放图像的方法而不是set_size_inches吗?