如何在animation.FuncAnimation()中传递参数以在曲面图中绘制条形图

时间:2019-02-25 16:16:11

标签: python matplotlib bar-chart mplot3d

我正在一个需要从串行监视器绘制数据的项目中 在尺寸为4x4的python中,随着传感器值的变化,该图应随时间变化

我只得到传感器感测到的第一个值,以后获得的值不会在绘图上更新。在关闭绘图后它会更新,新图自动显示并带有传感器的更新值,每次都会重复为了获得新的价值,我应该继续关闭

# plot 3d bar chart on python using serial data
import matplotlib.pyplot as plt
import mpl_toolkits.mplot3d.axes3d as p3
import matplotlib.animation as animation
import serial
from matplotlib import style
import numpy as np

style.use('classic')
dataarrray = []
data = []
cnt = 0
arduinoData = serial.Serial('COM5', 9600)


def update_bars(num, bars):
    # print(data)
    for i in range(0, 16):
        bars[i] = ax.bar3d(xpos[i], ypos[i], zpos[i], dx[i], dy[i], dz[i])
    return bars


while True:
   while (arduinoData.inWaiting() == 0):  # Wait here until there is data
      pass
   arduinoString = arduinoData.readline().decode().strip('\r\n')
   temp = float(arduinoString)
   dataarrray.append(temp)
   cnt += 1
   data.clear()

   if(len(dataarrray) >= 17):
       data = dataarrray[0:16]
       del dataarrray[0:16]

   if(len(data) == 16):
       print(data)
       fig = plt.figure()
       ax = p3.Axes3D(fig)
       xpos = [1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4]
       ypos = [1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4]
       zpos = np.zeros(16)
       dx = np.ones(16)
       dy = np.ones(16)
       dz = data

    # add bars
       bars = []
       for i in range(16):
            bars.append(ax.bar3d(xpos[i], ypos[i],
                             zpos[i], dx[i], dy[i], dz[i]))
       ax.set_title('3D bars')

       ax.set_xlabel('x axis')
       ax.set_ylabel('y axis')
       ax.set_zlabel('z axis')

       line_ani = animation.FuncAnimation(fig, update_bars, 20, fargs=[
                             bars], interval=100, blit=False)
       plt.show()

plot image obtained

0 个答案:

没有答案