python处理csv数据动态显示曲线实例代码

yipeiwu_com6年前Python基础

本文研究的主要是python处理csv数据动态显示曲线,分享了实现代码,具体如下。

代码:

# -*- coding: utf-8 -*- 
""" 
Spyder Editor 
 
This temporary script file is located here: 
C:\Users\user\.spyder2\.temp.py 
""" 
""" 
Show how to modify the coordinate formatter to report the image "z" 
value of the nearest pixel given x and y 
""" 
# coding: utf-8 
 
import time 
import string 
import os  
import math  
import pylab 
 
import numpy as np 
from numpy import genfromtxt 
import matplotlib 
import matplotlib as mpl 
from matplotlib.colors import LogNorm 
from matplotlib.mlab import bivariate_normal 
 
import matplotlib.pyplot as plt 
import matplotlib.cm as cm 
 
 
import matplotlib.animation as animation 
 
    
  
metric = genfromtxt('D:\export.csv', delimiter=',') 
 
lines=len(metric)  
#print len(metric) 
#print len(metric[4]) 
#print metric[4]  
 
rowdatas=metric[:,0] 
for index in range(len(metric[4])-1): 
  a=metric[:,index+1] 
  rowdatas=np.row_stack((rowdatas,a)) 
   
#print len(rowdatas) 
#print len(rowdatas[4]) 
#print rowdatas[4]  
#   
 
#plt.figure(figsize=(38,38), dpi=80) 
#plt.plot(rowdatas[4] ) 
#plt.xlabel('time') 
#plt.ylabel('value') 
#plt.title("USBHID data analysis") 
#plt.show() 
 
linenum=1 
##如果是参数是list,则默认每次取list中的一个元素,即metric[0],metric[1],...  
listdata=rowdatas.tolist() 
print listdata[4] 
 
#fig = plt.figure()  
#window = fig.add_subplot(111)  
#line, = window.plot(listdata[4] )  
  
fig, ax = plt.subplots() 
line, = ax.plot(listdata[4],lw=2) 
ax.grid() 
 
time_template = 'Data ROW = %d' 
time_text = ax.text(0.05, 0.9, '', transform=ax.transAxes) 
  
#ax = plt.axes(xlim=(0, 700), ylim=(0, 255))  
#line, = ax.plot([], [], lw=2)  
  
def update(data):  
  global linenum 
  line.set_ydata(data) 
#  print 'this is line: %d'%linenum 
  time_text.set_text(time_template % (linenum)) 
  linenum=linenum+1 
#  nextitem = input(u'输入任意字符继续: ') 
  return line,  
 
def init(): 
#  ax.set_ylim(0, 1.1) 
#  ax.set_xlim(0, 10) 
#  line.set_data(xdata) 
  plt.xlabel('time') 
  plt.ylabel('Time') 
  plt.title('USBHID Data analysis') 
  return line, 
   
ani = animation.FuncAnimation(fig, update,listdata , interval=1*1000,init_func=init,repeat=False)  
plt.show()  

总结

以上就是本文关于python处理csv数据动态显示曲线实例代码的全部内容,希望对大家有所帮助。感兴趣的朋友可以继续参阅本站其他相关专题,如有不足之处,欢迎留言指出。感谢朋友们对本站的支持!

相关文章

numpy判断数值类型、过滤出数值型数据的方法

numpy是无法直接判断出由数值与字符混合组成的数组中的数值型数据的,因为由数值类型和字符类型组成的numpy数组已经不是数值类型的数组了,而是dtype='<U11'。 1、ma...

python中将一个全部为int的list 转化为str的list方法

假设有这样一个List [1,2,3,4,5] 转化为下面这个样子 [‘1','2','3','4','5'] 解决方法一: a = [1,2,3] b = [ str(i) for...

使用Python获取网段IP个数以及地址清单的方法

使用Python获取网段IP个数以及地址清单的方法

使用Python获取网段的IP个数以及地址清单需要用到IPy的库,而相应的方法主要就是IP。 写小脚本如下: from IPy import IP ip = IP('192.1...

Python库urllib与urllib2主要区别分析

作为一个Python菜鸟,之前一直懵懂于urllib和urllib2,以为2是1的升级版。今天看到老外写的一篇《Python: difference between urllib and...

Python 私有函数的实例详解

Python 私有函数的实例详解 与大多数语言一样,Python 也有私有的概念: • 私有函数不可以从它们的模块外面被调用 • 私有类方法不能够从它们的类外面...