2017-07-18 93 views
-1

我有一个csv文件,包含以下列{time,x,y,z}和大约100000行。我想实时“实时流动/动画”数据,以便我可以了解发生的情况。如何在python中绘制/流数据来自csv文件?

尝试:

def generate_pieces(file, piecesize=128): 

piece = [] 
for i, line in enumerate(file): 
    if (i % piecesize == 0 and i > 0): 
     yield piece 
     del piece[:] 
    piece.append(line) 
yield piece 

file = csv.reader(open('file_name.csv')) 
for piece in generate_pieces(file): 
     x_data = [] 
     y_data = [] 
     z_data = []    
     t_vec = [] 

     for row in piece: 
      x_data.append(row[1]) 
      y_data.append(row[2]) 
      z_data.append(row[3]) 
      d = datetime.datetime.strptime(row[0], '%Y-%m-%d-%H%M%S') 
      d_simp = (d.hour+ (1/60)*d.minute + (1/3600)*d.second) 
      conversion = 86400*d.day+3600*d.hour+60*d.minute + d.second        
      t_vec.append(conversion)        

     x_data = [int(i) for i in x_data] # Converts values to int type.  
     y_data = [int(i) for i in y_data]   
     z_data = [int(i) for i in z_data] 

所以我尝试包括在现场的情节在同一时间服用在这里,我要流128点长度128的数据块?我不知道在哪里何去何从

+0

代码中没有动画。你有没有搜索“matplotlib动画”?结果对你有多大帮助? – ImportanceOfBeingErnest

回答

0

考虑使用matplotlib为了这个目的,它可以帮助情节数据的时间间隔与动画

示例代码

import matplotlib.pyplot as plot 
import matplotlib.animation as animation 

figure = plot.figure() 
axis= figure.add_subplot(1,1,1) 

def animateplots(i): 

    #Prepare data for plot 
    axis.clear() 
    axis.plot(x,y,z) 

ani = animation.FuncAnimation(figure, animateplots, interval=<intended interval>) 
plot.show() 

希望我对你的问题的理解是正确的,如果是这样的话,考虑阅读更多关于matplotlib的图形绘制需求

+0

感谢您的有用反馈。所以我应该将每件作品传递给我的for循环中的animateplots函数?谢谢 – Sjoseph

+0

单独配置间隔..一次传递所有数据..也可以通过使用传递数据的交错处理来测试绘图动画。看到行为 –

相关问题