Skip to content

FuncAnimation only triggered on assignment #18893

Closed

Description

@HaaLeo

Bug report

Bug summary

FuncAnimation gets only triggered/executed when it is assigned. For example _ = animation.FuncAnimation(*params) and anim = animation.FuncAnimation(*params) work, whereas animation.FuncAnimation(*params) does not work.

Code for reproduction

An example is the decay example from the matplotlib homepage.
I just changed ani = animation.FuncAnimation(fig, run, data_gen, interval=10, init_func=init) to animation.FuncAnimation(fig, run, data_gen, interval=10, init_func=init)

import itertools

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation


def data_gen():
    for cnt in itertools.count():
        t = cnt / 10
        yield t, np.sin(2*np.pi*t) * np.exp(-t/10.)


def init():
    ax.set_ylim(-1.1, 1.1)
    ax.set_xlim(0, 10)
    del xdata[:]
    del ydata[:]
    line.set_data(xdata, ydata)
    return line,

fig, ax = plt.subplots()
line, = ax.plot([], [], lw=2)
ax.grid()
xdata, ydata = [], []


def run(data):
    # update the data
    t, y = data
    xdata.append(t)
    ydata.append(y)
    xmin, xmax = ax.get_xlim()

    if t >= xmax:
        ax.set_xlim(xmin, 2*xmax)
        ax.figure.canvas.draw()
    line.set_data(xdata, ydata)

    return line,

animation.FuncAnimation(fig, run, data_gen, interval=10, init_func=init) # This is the only line different from the original example
plt.show()

Actual outcome

The animation does not start.

Expected outcome

The animation starts.

This problem only occurs for matplotlib==3.3.x. With matplotlib==3.2.2 this issue does not occur.

Matplotlib version

  • Operating system: macOS 10.15.7
  • Matplotlib version: 3.3.x
  • Matplotlib backend (print(matplotlib.get_backend())): TkAgg
  • Python version: 3.8.6
  • numpy version: 1.19.3

I installed phthon via homebrew and matplotlib in a venv via pip (pip3 install matplotlib)

Activity

  1. jklymak commented on Nov 4, 2020

    @jklymak
    Member

    I believe that is on purpose to avoid garbage collection of the animation.

  2. timhoffm commented on Nov 4, 2020

    @timhoffm
    Member

    It's documented here: https://matplotlib.org/devdocs/api/animation_api.html:

    In both cases it is critical to keep a reference to the instance object. The animation is advanced by a timer (typically from the host GUI framework) which the Animation object holds the only reference to. If you do not hold a reference to the Animation object, it (and hence the timers), will be garbage collected which will stop the animation.

    However that might not be very discoverable. We could additionally mention it in the class docstring and/or the examples themselves.

  3. HaaLeo commented on Nov 4, 2020

    @HaaLeo
    Author

    @timhoffm thx for clarification 👍🏼 . I only searched the changelog of v3.3.0 and could not find anything.

    Please feel free to close the issue then if you decide to not take further action.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions