2016-07-19 7 views
0

温度とdTリストを持つFIFOキューを実行していて、データをプロットしようとしています。私はチェックし、x、y、y1の長さはそれらが多くのインデックス値に達した後にすべて10であるので、if len(x)> 10:partが動作していることを知っています。しかし、私がプロットすると、リアルタイムプロットは予想通りに更新されません。これは本質的に、反復10で停止し、最も古いポイントを最新のポイントに置き換えるのではなく、60回の反復をすべて示します。それはプロットや他の何かの問題はコードですか?注:空のリストが呼び出される前に何かに注意を払わないでください。これは、機器が温度を読み取るためのちょっとしたコードなので、注意してください。あなたが動的にプロットを更新したい場合には、それは1に推奨されません先入れ先出しmatplotlibプロット

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

)各反復で、完全なプロットの設定を行うと、2)、例えば互いの上にプロットを描きます同じ位置に対して複数回plt.scatter(..)

This questionは、継続するための良い基礎を提供する必要があります。本質的には、プロットの作成&の構成とデータの設定を別々にする必要があります。&図面。それはすべてがダウンして最初に設定を行うと、図とそのプロットを覚えることになる

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à、作業をプロットして、このソリューションも非常に高速です。

(私はあなたにも今。動的軸を適応したいと思います、あなたは60に0の固定x軸を持っていますが、あなただけのスケール変更する必要があり、最後の10回の測定値をプロットしているとき)

+0

優れています。はい、私はx軸を変更する新しい方法を見つける必要があります。私はそれを0-10から始める方法を見つける必要があります。そして、10回後に1-11、2-12 ... 50-60、繰り返しとして –

+0

を実行します。 'plt.subplot(..)'によって返されます)。それらは 'set_xlim(..) 'という機能を提供します。ありがとう!http://stackoverflow.com/questions/17895698/updating-the-x-axis-values-using-matplotlib-animation – mhoff

+0

ありがとう!私はまだこのコードで動作するようにset_xlim()を取得しようとして苦労しているが、うまくいけば、それは十分にすぐに動作します –

関連する問題