【问题标题】:matplotlib.animation plotting inside a loopmatplotlib.animation 在循环内绘图
【发布时间】:2017-06-21 21:36:53
【问题描述】:

我通过来自三个传感器节点的套接字连接向我提供数据,温度、电压和湿度值各有三个子图。 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()

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

temp,volt,time 和 pres 是包含子列表的列表, 例如 - temp[0] 包含来自 node0 的温度值列表。

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

'temp_plot(时间[i],temp[i])
TypeError:'AxesSubplot' 对象不是 可调用的'

谁能帮我解决这个问题

【问题讨论】:

    标签: python matplotlib


    【解决方案1】:

    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])
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多