|
From: Jerzy K. <jer...@un...> - 2015-04-23 11:18:57
|
Le 23/04/2015 12:22, Virgil Stokes a écrit : > The following interesting example (random_data.py) is posted at: > > http://matplotlib.org/1.4.2/examples/animation/random_data.html > > > import matplotlib.pyplot as plt > import matplotlib.animation as animation import numpy as np > > fig, ax = plt.subplots() > line, = ax.plot(np.random.rand(10)) > ax.set_ylim(0, 1) > > def update(data): > line.set_ydata(data) > return line, > > def data_gen(): > while True: yield np.random.rand(10) > > ani = animation.FuncAnimation(fig, update, data_gen, interval=100) > plt.show() > > This codes works; but, I am very confused by it. For example: > > 1. There are 3 positional arguments given for animation.FuncAnimation; > but, in the > API documentation for this class > (http://matplotlib.org/api/animation_api.html), only > two positional arguments are shown. The third one is the third one, "/frames/ can be a generator, an iterable, or a number of frames." The name "data_gen" could suggest its meaning (after having read the doc). Note please that the keyword parameters are specified extra. > 2. data, the argument to the update function seems to be undefined. FuncAnimation usually passes the frame number: 0, 1, 2, ... as the first parameter of the update function, when "frames" is None, or the number of frames. If - as here - the third parameter is a generator, it passes the yielded data to update. It may be used or not. Please, in such cases test your programs by adding some simple tracing contraptions, say, print(data) inside update. Jerzy Karczmarczuk |