2017-06-21 87 views
0

我有三個傳感器節點通過套接字連接提供給我的數據,每個溫度,電壓和溼度值有三個子圖。溫度子圖應繪製每個節點的溫度值。同樣適用於溼度和電壓。這是我寫的代碼:matplotlib.animation在循環內繪圖

import socket 
import matplotlib.pyplot as plt 
import matplotlib.animation as animation 
import pickle 


fig = plt.figure() 
temp_plot = fig.add_subplot(3,1,1) 
volt_plot = fig.add_subplot(3,1,2) 
pres_plot = fig.add_subplot(3,1,3) 


maclist=['xyz01','xyz02','xyz03'] 
temp=[] 
volt=[] 
time=[] 
pres=[] 


ip = 'server IP address' 

sock = socket.socket(socket.AF_INET,socket.SOCK_STREAM) 
sock.connect((ip,10030)) 
print 'connection established to server...' 

def animate(i): 
    global sock 
    info = sock.recv(1024) 
    info = pickle.loads(info) 

    ind = maclist.index(info['MAC']) 
    if len(temp[ind]) == 10: #to make sure every time 10 points are plotted 
    temp[ind].pop(0) 
    time[ind].pop(0) 
    volt[ind].pop(0) 
    pres[ind].pop(0) 

    temp[ind].append(info['TEMP']) 
    time[ind].append(info['TIME']) 
    volt[ind].append(info['VOLT']) 
    pres[ind].append(info['PRES']) 
    temp_plot.clear() 
    volt_plot.clear() 
    pres_plot.clear() 

    for i in range(len(maclist)): 
    temp_plot(time[i],temp[i]) 
    volt_plot(time[i],volt[i]) 
    pres_plot(time[i],pres[i]) 

if __name__ == '__main__' : 
    ani = animation.FuncAnimation(fig,animate,interval = 100) 
    plt.show() 

info = {'MAC':'xyz01','TIME':21.4131,'TEMP':27.0,'VOLT':2.5,'PRES':892}

溫度,電壓,時間和PRES是包含子列表, 爲前列表 - 溫度[0]包含NODE0溫度值的列表。

此代碼給我一個錯誤,我懷疑是由於試圖循環

'temp_plot(time[i],temp[i])
TypeError: 'AxesSubplot' object is not callable '

會有人好心幫我對此

回答

0

temp_plot裏面情節是要繪製的插曲。您不能調用子圖本身。就像你不會寫plt(x,y)ax(x,y),而是寫plt.plot(x,y)ax.plot(x,y);這裏您需要

temp_plot.plot(time[i],temp[i]) 
volt_plot.plot(time[i],volt[i]) 
pres_plot.plot(time[i],pres[i])