在更新每个帧的行时,我在使用animation.FuncAnimation
时遇到了问题。
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
from sys import exit
#update set line a value(a list) of dict data
def update_line(num ,data, line):
line.set_data(data[num*0.1])
return line,
fig1 = plt.figure()
# just creat a dictionary of lists by variable length
data = {}
for i in range(20):
t = i*0.1
data[t] = [j*.5 for j in range(np.random.randint(2,5))]
print t , data[t]
l, = plt.plot([], [], 'r-')
line_ani = animation.FuncAnimation(fig1, update_line, 5, fargs=(data, l),
interval=500)
plt.show()
上面的代码给了我一个ValueError: too many values to unpack
感谢您的任何意见或指导。
答案 0 :(得分:1)
问题是您没有设置任何应该绘制ydata的xdata。正确的通话需要line.set_data(x, y)
。
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
#update set line a value(a list) of dict data
def update_line(num ,data, line):
y = data[num*0.1]
line.set_data(range(len(y)), y)
fig1 = plt.figure()
# just creat a dictionary of lists by variable length
data = {}
for i in range(20):
t = i*0.1
data[t] = [j*.5 for j in range(np.random.randint(2,5))]
l, = plt.plot([], [], 'r-')
line_ani = animation.FuncAnimation(fig1, update_line, 20, fargs=(data, l),
interval=500, repeat=True)
plt.xlim(0,5)
plt.ylim(0,2.5)
plt.show()