仅显示最近点数的情节

时间:2016-07-19 15:29:01

标签: python

我想制作一个关于温度与迭代的实时图,但我最终会得到这么多点,将它们放在同一个图上是没有意义的。有没有人知道有什么好的方法只能显示最近的(比方说100个)数据点,以便在前100个之后情节开始用新数据点替换旧数据点?

我认为没有代码会更容易,但这里是我现在的实时情节。

from time import sleep
from labjack import ljm
import pylab as pl
import matplotlib.pyplot as plt

# Open T7 over USB
handle = ljm.openS("T7", "USB", "ANY")

# Configure thermocouple line on AIN0
ljm.eWriteName(handle, "AIN0_EF_INDEX", 22)  # Feature index for type K thermocouple
ljm.eWriteName(handle, "AIN0_EF_CONFIG_A", 1)  # Units. Default = Kelvin. 1 = Celsius. 2 = Fahrenheit.
ljm.eWriteName(handle, "AIN0_EF_CONFIG_B", 60052)  # CJC source, address for device temperature sensor
ljm.eWriteName(handle, "AIN0_EF_CONFIG_D", 1.0)  # Slope for CJC reading
ljm.eWriteName(handle, "AIN0_EF_CONFIG_E", 0.0)  # Offset for CJC reading

temperature = []
x = list()
y = list()
x1 = list()
y1 = list()
dT_tol = .5

plt.ion()
fig=plt.figure()

# Read loop
for i in range(60):
    # Get the thermocouple reading on AIN0. 
    tempC = ljm.eReadName(handle, "AIN0_EF_READ_A")
    temperature.append(tempC)
    dT = temperature[i]-temperature[i-1]

    if -dT_tol<dT<dT_tol:
        print "Temperature:","%.3f"% temperature[i],"         " "dT:", "%.3f"% dT, "         " "Steady State" 
        sleep(1)
    else:
        print "Temperature:","%.3f"% temperature[i],"         " "dT:", "%.3f"% dT
        sleep(1) 



    #Plotting
    plt.figure(1)   
    plt.subplot(211)
    plt.axis([0,60,0,80])
    x.append(i)
    y.append(temperature[i])
    plt.scatter(x,y)
    plt.ylabel('Temperature (C)')

    plt.subplot(212)
    plt.axis([0,60,-4,4])
    x1.append(i)
    y1.append(dT)
    plt.scatter(x1,y1,zorder = 2)

    #Set dT steady state boundaries
    plt.axhspan(-dT_tol, dT_tol, color='#87CEFA', alpha=1, zorder = 1)

    plt.ylabel('dT')
    plt.xlabel('Time (s)')
    plt.show()
    plt.pause(.0001)

# Close handle
ljm.close(handle)

2 个答案:

答案 0 :(得分:0)

您可以使用数组列表显示一段时间内给出的所有数据。 例如

tempaturelist = []

for i in range(50):

      enter code here

tempaturelist.append(tempature)
print tempaturelist

如果对所有值使用相同的变量,则会进行覆盖。 这就是为什么你只看到最近的价值观。

答案 1 :(得分:0)

编辑:

您可以考虑使用deque对象来提高性能。它就像一个堆栈/队列混合,可能比numpy.roll更快。我把旧代码留在了例如..

from collections import deque

你可以使用这样的东西,只需更新它以满足你的需求(我只是要编写随机数据,因为我太懒了,不能使用你的例子)

import numpy as np
import pylab as plt

buffer_size = 100 # how many data points you want to plot at any given time
#data_buffer = np.zeros( buffer_size) # this is where you will keep the latest data points
data_buffer = deque()
for i in range( buffer_size):
    data_buffer.append(0)

temperatures = np.random.random( 200 ) # some random temperature data, i dunno

# setup the figure
fig = plt.figure(1)
plt.suptitle('Previous %d temperatures'%buffer_size, fontsize=12)
ax = plt.gca()

for i,Temp in enumerate( temperatures ):
    #data_buffer = np.roll( data_buffer, shift=-1)
    #data_buffer[ -1] = Temp
    data_buffer.popleft()
    data_buffer.append( Temp)
    ax.plot( data_buffer, 'rs', ms=6) # whatever style you want...
    plt.draw()
    plt.pause(0.01)
    plt.cla() # clears the axis 

我不会发布此剧情的输出,因为它总是会改变,但是自己尝试一下;)