我已经创建了用于实时绘制接收数据的代码。代码运行正常。但是过了一段时间,出现“ ValueError:没有足够的值要解包(预期4,得到1)”错误,实时动画卡住了。
这是我的完整代码:`
import matplotlib.pyplot as plt
import matplotlib.animation as animation
from matplotlib import style
style.use('fivethirtyeight')
fig = plt.figure()
ax1 = fig.add_subplot(1,1,1)
#ax2 = fig.add_subplot(2,1,2)
def animate(i):
graph_data = open('C:/navikaUI/Session~1/Rawdata.log','r').read()
lines = graph_data.split('\n')
Time = []
Latt = []
Long = []
for line in lines:
if len(line) > 1:
if line.startswith( '$IRGGA' ):
time, lat, _, lon = line.split(',')[1:5]
try:
time = float (time)
lat = float ( lat )/100
lon = float ( lon )/100
Latt.append(lat)
Long.append(lon)
Time.append(time)
except:
pass
ax1.clear()
#ax2.clear()
ax1.plot(Time, Latt, c= 'b', label= 'latitude', linewidth= 1)
ax1.set_xlabel('Time')
ax1.set_ylabel('Lat')
#ax2.plot(Time, Long, c= 'r', label= 'longitude', linewidth= 1)
#ax2.set_xlabel('Time')
#ax2.set_ylabel('Long')
ax1.legend()
#ax2.legend()
ani = animation.FuncAnimation(fig, animate, interval=1000)
plt.show()
从绘图开始几分钟后,time, lat, _, lon = line.split(',')[1:5]
这行代码发生错误。
如何避免此错误?请帮忙。谢谢。