我已编写此代码以从csv文件创建图形,并希望在csv文件更新时不断更新图形。它目前正在工作,但我有几个问题。 1.如何自动更新轴而不是静态更新轴 2.地块的颜色(2行)不断变化?这是因为它每次重新绘制它们吗? 这是最好的方法吗? 无论如何还要加快速度吗?
#!/usr/bin/env python
import time
from pylab import *
plt.ion() # turn interactive mode on
# initial data
na_data = np.loadtxt('/home/pi/Desktop/real_time/ram/data_temp.csv', delimiter=',', skiprows=0, usecols=None)
na_temp1_x = na_data[::2,0] #every odd row x axis
na_temp2_x = na_data[1::2,0] #every even row x axis
na_temp1_y = na_data[::2,1] #every odd row
na_temp2_y = na_data[1::2,1] #every even row
# initial plot
plt.plot(na_temp1_x, na_temp1_y, label="Temp 1") #plots graph for temp sensor 1
plt.plot(na_temp2_x, na_temp2_y, label="Temp2") #plots graph for temp sensor 2
plt.axis([0, 100, 0, 40])
plt.legend(loc="upper left") #puts legend in top left
plt.ylabel('Temp')
plt.xlabel('Time')
grid()
draw()
while True:
time.sleep(1) # delays for 1 seconds
# update data
na_data = np.loadtxt('/home/pi/Desktop/real_time/ram/data_temp.csv', delimiter=',', skiprows=0, usecols=None)
na_temp1_x = na_data[::2,0] #every odd row x axis
na_temp2_x = na_data[1::2,0] #every even row x axis
na_temp1_y = na_data[::2,1] #every odd row
na_temp2_y = na_data[1::2,1] #every even row
#plt.relim()
#plt.autoscale_view(True,True,True)
# initial plot
plt.plot(na_temp1_x, na_temp1_y, label="Temp 1") #plots graph for temp sensor 1
plt.plot(na_temp2_x, na_temp2_y, label="Temp2") #plots graph for temp sensor 2
draw()
#time.sleep(30)