尝试创建一个程序来读取串行数据并更新多个图形(目前有 1 个折线图和 2 个条形图,但可能会更多)。
现在使用 3 个单独的 FuncAnimation() 调用,但事实证明速度非常慢,这不好,因为我仍然需要在将来添加更多动画人物的选项。
那么我怎样才能使它成为一个单一的 FuncAnimation (或者类似的东西)来更新所有三个(可能更多)数字呢?或者,我可以做些什么来加快速度呢?
#figure for current
amps = plt.figure(1)
ax1 = plt.subplot(xlim = (0,100), ylim = (0,500))
line, = ax1.plot([],[])
ax1.set_ylabel('Current (A)')
#figure for voltage
volts = plt.figure(2)
ax2 = plt.subplot()
rects1 = ax2.bar(ind1, voltV, width1)
ax2.grid(True)
ax2.set_ylim([0,6])
ax2.set_xlabel('Cell Number')
ax2.set_ylabel('Voltage (V)')
ax2.set_title('Real Time Voltage Data')
ax2.set_xticks(ind1)
#figure for temperature
temp = plt.figure(3)
ax3 = plt.subplot()
rects2 = ax3.bar(ind2, tempC, width2)
ax3.grid(True)
ax3.set_ylim([0,101])
ax3.set_xlabel('Sensor Number')
ax3.set_ylabel('temperature (C)')
ax3.set_title('Real Time Temperature Data')
ax3.set_xticks(ind2)
def updateAmps(frameNum):
try:
#error check for bad serial data
serialString = serialData.readline()
serialLine = [float(val) for val in serialString.split()]
print (serialLine)
if (len(serialLine) == 5):
voltV[int(serialLine[1])] = serialLine[2]
tempC[int(serialLine[3])] = serialLine[4]
currentA.append(serialLine[0])
if (len(currentA)>100):
currentA.popleft()
line.set_data(range(100), currentA)
except ValueError as e:
#graphs not updated for bad serial data
print (e)
return line,
#function to update real-time voltage data
def updateVolts(frameNum):
for rects, h in zip(rects1,voltV):
rects.set_height(h)
return rects1
#function to update real-time temperature data
def updateTemp(frameNum):
for rects, h in zip(rects2,tempC):
rects.set_height(h)
return rects2
调用 funcAnimation:
anim1 = animation.FuncAnimation(amps, updateAmps,
interval = 20, blit = True)
anim2 = animation.FuncAnimation(volts, updateVolts, interval = 25, blit = True)
anim3 = animation.FuncAnimation(temp, updateTemp, interval = 30, blit = True)