2

I'm trying to use FuncAnimation, but encounter the problem that it stops after displaying only a single frame.

This problem has been posted before on StackOverflow,

Python Matplotlib FuncAnimation only draws one frame

FuncAnimation printing first image only

However, the solution there -- to give a reference to the animation object -- does not do the trick in my case and in both cases leaves blank plots when I plot them.

To give an example, I would expect this code, taken from https://riptutorial.com/matplotlib/example/23558/basic-animation-with-funcanimation to animate a red particle tracing out a sine curve.

However, in my case the red particle gets stuck at t=0. After running the code below, number_of_calls has been increased to 1, and so animate() has only been called once. For reference, I'm using Python 3.6.5.

Any help would be very much appreciated!

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

TWOPI = 2*np.pi

fig, ax = plt.subplots()

t = np.arange(0.0, TWOPI, 0.001)
s = np.sin(t)
l = plt.plot(t, s)

ax = plt.axis([0,TWOPI,-1,1])

redDot, = plt.plot([0], [np.sin(0)], 'ro')

number_of_calls=0

def animate(i):

    global number_of_calls

    redDot.set_data(i, np.sin(i))
    number_of_calls+=1
    return redDot,

# create animation using the animate() function
myAnimation = FuncAnimation(fig, animate, frames=np.arange(0.0, TWOPI, 0.1), \
                                      interval=10, blit=True, repeat=True)

plt.show()
user810643
  • 41
  • 5
  • The code works fine for me. I suspect it's a backend problem. Which backend are you using? – ImportanceOfBeingErnest Jun 27 '19 at 15:10
  • Not sure how to answer that. I'm executing the code in Jupyter, loaded from anaconda-navigator. The underlying Python version is 3.6.5. Does that address your question about the backend I'm using? Otherwise, how can I check it? – user810643 Jun 27 '19 at 15:14
  • `plt.get_backend()`. Anyways, the fact that you use jupyter answers this. Jupyter inline backend produces a static png image, right? So that cannot have moving dots in it. – ImportanceOfBeingErnest Jun 27 '19 at 15:17
  • I'm puzzled: in lecture 4 at https://machine-learning-for-physicists.org/ from 26:04 and onwards, the slides show the progressive learning of an image by a neural network, using FuncAnimation, and run in Jupyter. The code above is a shorter example of the code in the lecture, but seems to have the same feature in my version of Jupyter. So Jupyter does seem to be able to do animation. I wonder if the issue can be resolved by loading some additional library or perhaps adding some additional code? – user810643 Jun 27 '19 at 15:30
  • I closed this question as duplicate of a question where you find all your options. (See yellow box) – ImportanceOfBeingErnest Jun 27 '19 at 15:36
  • Indeed, setting (e.g.) ``%matplotlib notebook`` before loading matplotlib solves my problem for the above code. (And ``%matplotlib tk`` for the code in the lectures. Thanks for your help, ImportanceOfBeingEarnest! – user810643 Jun 27 '19 at 16:02

0 Answers0