我正在尝试创建一个程序,它可以读取串行数据并更新多个图形(目前有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)