python - python中时间序列数据的螺旋图
问题描述
我有几天间隔 10 分钟的时间序列数据。我想在 python 中绘制这些数据,就像一个螺旋图,看起来像一个 24 小时内的时钟(圆的半径是 24 小时)在这种情况下,作为同心圆的日子,开始日期 2019-10-08 作为最内圈和2019-10-09 作为下一个圈,依此类推。值应根据“值”列进行颜色编码。螺旋图应该看起来像这个 螺旋图 有人可以帮我吗?
TimeStampLocal Value
2019-10-08 14:30:00 8
2019-10-08 14:40:00 19
2019-10-08 14:50:00 14
2019-10-08 15:00:00 22
2019-10-08 15:10:00 11
2019-10-08 15:20:00 9
2019-10-08 15:30:00 6
2019-10-08 15:40:00 7
2019-10-08 15:50:00 9
2019-10-08 16:00:00 19
2019-10-08 16:10:00 7
2019-10-08 16:20:00 19
2019-10-08 16:30:00 9
2019-10-08 16:40:00 95
2019-10-08 16:50:00 64
2019-10-08 17:00:00 23
2019-10-08 17:10:00 12
2019-10-08 17:20:00 17
2019-10-08 17:30:00 68
2019-10-08 17:40:00 6
2019-10-08 17:50:00 9
2019-10-08 18:00:00 27
解决方案
这应该有效。虽然它不是最好的程序并且可能需要一些重构。
但它有效并显示了将时间戳转换为径向坐标并将它们显示在画布上的总体思路。
import time
import math
from tkinter import *
class Spiralplot(Frame):
def __init__(self, master=None, d = [], width = 500, height=500):
Frame.__init__(self,master)
top=self.winfo_toplevel() #Flexible Toplevel of the window
top.rowconfigure(0, weight=1)
top.columnconfigure(0, weight=1)
self.grid(sticky=N+S+W+E)
x,y = int(width/2),int(height/2)
self.plotregion = Canvas(self,width=width,height=height,bg="#ffffff")
self.plotregion.grid(row=0,column=0)
self.plotregion.create_line(x-10,y,x+10,y)
self.plotregion.create_line(x,y-10,x,y+10)
lines = []
for p in d:
xpos = x+math.cos(p[3])*p[2]
ypos = y+math.sin(p[3])*p[2]
lines.append((xpos,ypos,p[4]))
if len(lines)>0:
for i in range(len(lines)-1):
print(lines[i][0],lines[i][1],lines[i+1][0],lines[i+1])
self.plotregion.create_line(lines[i][0],lines[i][1],lines[i+1][0],lines[i+1][1],
fill=lines[i][2],width=2)
def rainbow(x):
## Compute rainbow color from a number between 0 and 1
if x >1:x=1
if x <0:x=0
red = math.cos(x*2*math.pi)+0.5
green = math.cos((x+.66)*2*math.pi)+.05
blue = math.cos((x+.33)*2*math.pi)+0.5
outstr = '#'
for v in [red,green,blue]:
if v > 1:
outstr+="ff"
elif v < 0:
outstr+="00"
else:
h = hex(int(v*255)).split('x')[-1]
if len(h)==1:
outstr+="0"+h
else:
outstr+=h
return outstr
def position(tstamp, firstday="2019-10-07",r1=100,step=20):
## Transforms a timestamp to radial coordinates. Add a first day and its radius to customize
## r1: Radius of the first day
## step: Step in radius between days
tfirst = time.strptime(firstday,"%Y-%m-%d")
t = time.strptime(tstamp,"%Y-%m-%d %H:%M:%S")
daytime = (t[3]+t[4]/60)/24
delta_d = (time.mktime(t)-time.mktime(tfirst))//(24*3600)
radius = r1 + (delta_d+daytime)*step
angle = daytime*2*math.pi
return (radius, angle)
def add_coordinates(data):
for i in range(len(data)):
p = position(data[i][0])
col = rainbow(data[i][1]/100)
data[i] = (data[i][0],data[i][1],p[0],p[1],col)
return data
labels = ("TS", "Value")
data = [("2019-10-08 14:30:00",8),
("2019-10-08 14:40:00",19),
("2019-10-08 14:50:00",14),
("2019-10-08 15:00:00",22),
("2019-10-08 15:10:00",11),
("2019-10-08 15:20:00",9),
("2019-10-08 15:30:00",6),
("2019-10-08 15:40:00",7),
("2019-10-08 15:50:00",9),
("2019-10-08 16:00:00",19),
("2019-10-08 16:10:00",7),
("2019-10-08 16:20:00",19),
("2019-10-08 16:30:00",9),
("2019-10-08 16:40:00",95),
("2019-10-08 16:50:00",64),
("2019-10-08 17:00:00",23),
("2019-10-08 17:10:00",12),
("2019-10-08 17:20:00",17),
("2019-10-08 17:30:00",68),
("2019-10-08 17:40:00",6),
("2019-10-08 17:50:00",9),
("2019-10-08 18:00:00",27)]
data = add_coordinates(data)
root = Tk()
plot = Spiralplot(root,d = data)
root.mainloop()
您提供的示例数据的输出将是: