0

我有多个数据源在特定时间戳报告真/假值,如下所示:

{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 之间的时间段着色。

目前我有这个:

时间线

在现实中,有更多的时间戳和更多的数据源,但它始终是相同的过程。

我正在使用带有以下脚本的 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?

4

0 回答 0