在Python中,我有一个代码放在一起,从CSV中提取数据(csv如下所示:)
TimeStamp, ReadCount, Antenna, Protocol, RSSI, EPC, Sensor
09/21/2016 15:24:40.560, 5499, 1, GEN2, -21, E036112D912508B3, 23.78,47.00,0.00,2.21, (Infinity%)
09/21/2016 15:24:41.138, 5506, 1, GEN2, -9, E036112D912508B3, 23.99,46.00,0.00,2.26, (Infinity%)
09/21/2016 15:24:41.623, 5513, 1, GEN2, -25, E036112D912508B3, 23.99,46.00,0.00,2.26, (Infinity%)
09/21/2016 15:24:42.120, 5520, 1, GEN2, -18, E036112D912508B3, 23.78,46.00,0.00,2.26, (Infinity%)
09/21/2016 15:24:42.633, 5527, 1, GEN2, -12, E036112D912508B3, 23.99,45.00,0.00,2.23, (Infinity%)
09/21/2016 15:24:43.211, 5534, 1, GEN2, -9, E036112D912508B3, 23.99,46.00,0.00,2.26, (Infinity%)
09/21/2016 15:24:43.744, 5541, 1, GEN2, -16, E036112D912508B3, 23.99,46.00,0.00,2.26, (Infinity%)
然后它会绘制这些数据 - 这是代码:
import matplotlib
import matplotlib.pyplot as plt
import matplotlib.animation as animation
from datetime import datetime
offset = -7.4954
slope = 0.9548
fig = plt.figure(facecolor='#07000d')
ax1 = fig.add_subplot(111, axisbg='#07000d')
ax1.spines['bottom'].set_color("#5998ff")
ax1.spines['top'].set_color("#5998ff")
ax1.spines['left'].set_color("#5998ff")
ax1.spines['right'].set_color("#5998ff")
def animate(i):
graph_data = open('SensorLog.csv','r').read()
dataArray = graph_data.split('\n')
xar=[]
yar=[]
for eachLine in dataArray:
if 'TimeStamp' not in eachLine:
if len(eachLine)>1:
t,rc,ant,prot,rssi,epc,temp,ten,powr,unpowr,inf=(eachLine.split(','))
time = datetime.strptime(t, '%m/%d/%Y %H:%M:%S.%f')
clock = time.strftime('%I:%M')
xs = matplotlib.dates.datestr2num(clock)
hfmt = matplotlib.dates.DateFormatter('%m/%d/%Y\n%I:%M:%S %p')
# Convert tension
tension = int(float(ten)*float(slope)+float(offset))
xar.append(xs)
yar.append(tension)
ax1.clear()
ax1.grid(True, color='w')
plt.ylabel('Tension (lb)',color='w', fontsize=20)
plt.title('Spiral 1 Tension',color='w', fontsize=26)
ax1.tick_params(axis='y', colors='w')
ax1.tick_params(axis='x', colors='w')
ax1.xaxis.set_major_formatter(hfmt)
fig.autofmt_xdate()
ax1.plot (xar,yar, 'c', linewidth=2)
ani = animation.FuncAnimation(fig, animate, interval=10000)
plt.show()
我希望能够让x轴只显示过去90秒的数据,而不是不断增长以适应所有新数据,而只是将旧数据推出超过90秒范围。例如,它应该显示0-90秒的点,一秒后它将显示1-91秒。
我见过一些例子但似乎没有什么对我来说有用。有什么帮助吗?