Matplotlib animation FuncAnimation frame argument

I use matplotlib animation, calling:

plot = animation.FuncAnimation(fig, update, frames=data_gen(a), init_func=init, interval=10, blit=True)

Here "a" is the initial value for the data_gen function, which looks like this:

data_gen(x)
    old_x = x
    while True:
        new_x = func(old_x)
        old_x = new_x
        yield new_x

The purpose of this code is for data_gen to create a new value for new_x every time the animated plot is updated.

BUT ... this happens instead:

animation.py raises an error in the init () method of the FuncAnimation class.

The problem occurs in this code:

elif iterable(frames):
    self._iter_gen = lambda: iter(frames)
    self.save_count = len(frames)

Error: "TypeError: object of type" generator "does not have len ()"

It looks like data_gen iserable, but it does not have len ().

Here is the init () code in the FuncAnimation class:

    # Set up a function that creates a new iterable when needed. If nothing
    # is passed in for frames, just use itertools.count, which will just
    # keep counting from 0. A callable passed in for frames is assumed to
    # be a generator. An iterable will be used as is, and anything else
    # will be treated as a number of frames.
    if frames is None:
        self._iter_gen = itertools.count
    elif isinstance(frames, collections.Callable):
        self._iter_gen = frames
    elif iterable(frames):
        self._iter_gen = lambda: iter(frames)
        self.save_count = len(frames)
    else:
        self._iter_gen = lambda: iter(list(range(frames)))
        self.save_count = frames

, data_gen .Callable. , len (frames) .

, , !

+4
1

, A) ( ), .. data_list = list(data_gen) B) , : PR # 2634 C) matplotlib , -

C) ;)

# copied directly from the proposed fix
def monkey_patch_init(self, fig, func, frames=None, init_func=None, fargs=None,
             save_count=None, **kwargs):
    if fargs:
        self._args = fargs
    else:
        self._args = ()
    self._func = func

    # Amount of framedata to keep around for saving movies. This is only
    # used if we don't know how many frames there will be: in the case
    # of no generator or in the case of a callable.
    self.save_count = save_count

    # Set up a function that creates a new iterable when needed. If nothing
    # is passed in for frames, just use itertools.count, which will just
    # keep counting from 0. A callable passed in for frames is assumed to
    # be a generator. An iterable will be used as is, and anything else
    # will be treated as a number of frames.
    if frames is None:
        self._iter_gen = itertools.count
    elif six.callable(frames):
        self._iter_gen = frames
    elif iterable(frames):
        self._iter_gen = lambda: iter(frames)
        if hasattr(frames, '__len__'):
            self.save_count = len(frames)
    else:
        self._iter_gen = lambda: xrange(frames).__iter__()
        self.save_count = frames

    # If we're passed in and using the default, set it to 100.
    if self.save_count is None:
        self.save_count = 100

    self._init_func = init_func

    # Needs to be initialized so the draw functions work without checking
    self._save_seq = []

    TimedAnimation.__init__(self, fig, **kwargs)

    # Need to reset the saved seq, since right now it will contain data
    # for a single frame from init, which is not what we want.
    self._save_seq = []

monkey_patch_init, ( ) . buggy __init__ :

matplotlib.animation.FuncAnimation.__init__ = monkey_patch_init

.

ani = animation.FuncAnimation(fig, update, frames=data_gen(a), init_func=init, interval=10, blit=True)

, plot . pyplot (, ipython --pylab), plotmatplotlib.pyplot.plotplt.gca().plot, , -.

+2

All Articles