我需要你的幫助,解決我最近處理它的問題。 我可以繪製從我的手機藍牙傳輸並通過我的筆記本電腦的COM端口接收的串行數據。乍一看,它似乎是好的,但最多它可以每260毫秒(〜3 fps)繪製。但手機每100毫秒發送一次數據。我很確定這個問題來源於「情節」和「身材」命令,這讓我感到困惑。我很感激,如果有人可以糾正我的代碼:在Matplotlib中使用動畫功能緩慢繪圖,Python
from Tkinter import *
import serial
import matplotlib.pyplot as plt
import matplotlib.animation as animation
fig = plt.figure()
ax1 = fig.add_subplot(1, 1, 1)
ser = serial.Serial("COM4", baudrate=115200, timeout=0.1)
cnt=0
xComponent=[]
plt.ylim(0,30)
while (ser.inWaiting() == 0): # Wait here until there is data
pass
def animate(i):
BluetoothString = ser.readline()
ser.flush()
dataArray = BluetoothString.split(',')
x = float(dataArray[2]) # we only need 3rd component
xComponent.append(x)
print xComponent
ax1.clear()
ax1.plot(xComponent)
plt.ylim(0,25)
global cnt
if (cnt > 16):
xComponent.pop(0)
else:
cnt = cnt + 1
ani = animation.FuncAnimation(fig, animate, interval=0)
plt.show()
這段代碼什麼都不做。 「FuncAnimation」在哪裏? – furas
@furas它來自[mpl](http://matplotlib.org/api/animation_api.html#matplotlib.animation.FuncAnimation) – Aaron
我知道'FuncAnimation'是來自'mpl',但我確實在你的代碼中看到它。但我看到你改變了代碼。 – furas