Q:

Plot a dictionary of lists by animation.FuncAnimation

I have a problem in using animation.FuncAnimation, in updating line for each frame.
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()

The above code gives me a ValueError: too many values to unpack.
Thanks for any comments or guide.

A:

The problem is that you don't set any xdata against which the ydata should be plotted. The correct call needs to be 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()

