先进先出的matplotlib绘图

时间:2016-07-19 21:30:21

标签: python matplotlib

我正在使用温度和dT列表进行FIFO队列,我正在尝试绘制数据。我已经检查过,x,y,y1的长度在达到那么多索引值后都是10,所以我知道if len(x)> 10:部分是否有效。但是,当我绘制时,实时绘图不会像我期望的那样更新。它基本上显示了所有60次迭代,而不是在迭代10处停止,而只是用最新点替换最旧的点。这是一个问题与绘图或其他东西是代码?注意:在空列表调用之前不要注意任何事情,因为这只是让设备读取温度的labjack代码。

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()
y1 = list()
li = 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]

    x.append(i)
    y.append(temperature[i])
    y1.append(dT)

    if len(x)>10:
        del x[0]
        del y[0]
        del y1[0]


    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)

    #print(len(x),len(y),len(y1))
    #sleep(1)

    #Plotting
    plt.figure(1)   
    plt.subplot(211)
    plt.axis([0,60,0,80])

    plt.scatter(x,y)
    plt.ylabel('Temperature (C)')

    plt.subplot(212)
    plt.axis([0,60,-4,4])
    plt.scatter(x,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)

1 个答案:

答案 0 :(得分:1)

当您想要动态更新绘图时,不建议1)在每次迭代中进行完整的绘图配置,2)将绘图绘制在彼此之上,例如: plt.scatter(..)多次为同一位置。

This question应该为您提供良好的基础。从本质上讲,你必须将情节创作和创作分开。配置和设置数据&amp;图。

这是一个非常小(不太干净)的工作示例,您可以从以下开始:

from time import sleep
import matplotlib.pyplot as plt

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

plt.ion()
fig = plt.figure(1)
plt.subplot(211)

temp_plot = plt.scatter([],[])
plt.axis([0,60,0,80])
plt.ylabel('Temperature (C)')

plt.subplot(212)
delta_temp_plot = plt.scatter([],[],zorder = 2)
plt.axis([0,60,-4,4])
plt.axhspan(-dT_tol, dT_tol, color='#87CEFA', alpha=1, zorder = 1)
plt.ylabel('dT')
plt.xlabel('Time (s)')

plt.show()

# Read loop
for i in range(60):
    tempC = i * 3
    temperature.append(tempC)
    dT = temperature[i]-temperature[i-1]

    x.append(i)
    y.append(temperature[i])
    y1.append(dT)

    if len(x)>10:
        del x[0]
        del y[0]
        del y1[0]

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

    temp_plot.set_offsets(zip(x, y))
    delta_temp_plot.set_offsets(zip(x,y1))

    fig.canvas.draw()
    fig.canvas.flush_events()

这一切都归结为在开始时进行配置并记住图形及其图形。然后,接收数据,将其馈送到图表,然后可以重新绘制图形。 Voilà,绘图作品和这个解决方案也快得多。

(我想你也想动态调整轴。现在,你有一个0到60的固定x轴,但是当你只绘制最后十个测量时你必须重新缩放)