Python 随着时间的推移,图表显示来自多个来源的真/假值
我有多个数据源在特定时间戳报告真/假值,如下所示:Python 随着时间的推移,图表显示来自多个来源的真/假值,python,matplotlib,Python,Matplotlib,我有多个数据源在特定时间戳报告真/假值,如下所示: {1338: [ (1377259958, False), (1378703557, True)], 1343: [ (1377259911, True), (1377812511, False), (1377814321, True)], 1354: [ (1377260040, False), (1377296033, True), (1377382446, False),
{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轴上。每个数据源都应为数据从真变假之间的时间段着色
目前,我有以下几点:
事实上,有更多的时间戳和更多的数据源,但它总是相同的过程
我将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')
def时间线(y、xstart、xstop、ok):
颜色='r'如果确定,则为'g'
打印“图形”、y、xstart、xstop、颜色
plt.hlines(y,xstart,xstop,color,lw=3)
plt.vlines(xstart,y+0.45,y-0.45,彩色,lw=0.5)
plt.V线(xstop,y+0.45,y-0.45,彩色,lw=0.5)
最大时间=0
mintime=time()
对于探测,timeline.iteritems()中的tl:
newmax=max(tl,key=itemgetter(0))[0]
newmin=min(tl,key=itemgetter(0))[0]
如果newmax>maxtime:
打印“新的最大时间:%s(旧的:%s)”%(新的最大时间,最大时间)
maxtime=newmax
如果newmin 如果ts[0],您尝试过什么?我可以想出几种方法来轻松地使用matplotlib
(Patch.Rectangle
或粗线条),但正如你所质疑的那样,它读起来就像是免费咨询的请求。我尝试用plot_date函数为每个数据源的每个时间戳绘制图表,但这是不可读的。我找不到要使用的正确绘图类型。我不认为“如何绘制此类数据的图表”是在寻找免费的咨询服务?那就告诉我们吧。在你的问题中,你没有在提问前先尝试的证据。如果你说“这就是我尝试过的,但没有成功”,人们的反应会比你说“我该怎么做”要好得多