matplotlib.animation在循环中绘制

2024-06-09 13:30:14 发布

您现在位置:Python中文网/ 问答频道 /正文

我有三个传感器节点通过插座连接提供给我的数据,每个节点有三个子批次,分别用于温度、电压和湿度值。temp子图应该绘制每个节点的温度值。湿度和电压也是如此。下面是我写的代码:

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}

temp、volt、time和pres是包含子列表的列表, for ex-temp[0]包含节点0的温度值列表。在

这段代码给了我一个错误,我怀疑这是由于试图在循环中绘制

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

有谁能帮我一下吗


Tags: importinfo节点timeplotfigsocket温度
1条回答
网友
1楼 · 发布于 2024-06-09 13:30:14

temp_plot是要绘制到的子批次。不能调用子块本身。正如您不会写plt(x,y)ax(x,y),而是plt.plot(x,y)或{};在这里您需要

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

相关问题 更多 >