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

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')

def animate(i):
    redDot.set_data(i, np.sin(i))
    return redDot,

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

plt.show()

This is code from https://riptutorial.com/matplotlib/example/23558/basic-animation-with-funcanimation The desired output it shown on that website. It's supposed to show a dot moving across a sine wave. When I run the code I just get a dot at the beginning of the sine wave, without any animation. I've tried it in Spyder and Jupyter with the same result in each. Why isn't this working?

EDIT: Adding "%matplotlib notebook" has fixed it. However now I want to download my animation, and I tried the following

myAnimation.save('dynamic_images.mp4')

But I got the error message "MovieWriter ffmpeg unavailable; trying to use instead." I made sure to install ffmpeg with

pip install ffmpeg

But that hasn't helped. How do I download animations with Jupyter Notebook? I also tried

from IPython.display import HTML
HTML(ani.to_html5_video())

and I get an error again because "Requested MovieWriter (ffmpeg) not available"

Dylan Silvas
  • 33
  • 1
  • 5
  • 2
    Because you need to use an interactive backend. One of `TkAgg`, `Qt5Agg`, `nbagg` (aka. `%matplotlib notebook`), etc. Check [animation-in-ipython-notebook](https://stackoverflow.com/questions/35532498/animation-in-ipython-notebook/46878531#46878531) for options with jupyter. – ImportanceOfBeingErnest Dec 06 '19 at 02:00
  • Thank you, I added that to the top of the code and it didn't work at first but it did when I ran it a second time. Out of curiosity, is there an IDE that doesn't require interactive backends by default? I thought Spyder was like that but I really don't know anything. I'd like to just code without always worrying about interactivity complications. – Dylan Silvas Dec 06 '19 at 02:06
  • @ImportanceOfBeingErnest Do you have any idea what else I am doing wrong here? – Dylan Silvas Dec 06 '19 at 04:54
  • In Spyder you can select the backend also in the settings. But you can also select it via code `matplotlib.use("TkAgg")`. In order to save a video you need ffmpeg in your system path. Googling for something like "how to install ffmpeg" should help. – ImportanceOfBeingErnest Dec 06 '19 at 13:00
  • I have the same issue :( – Daniel Vilas-Boas Jun 30 '20 at 19:54
  • My guess would be, you have to set the path to your ffmpeg executable with: `plt.rcParams['animation.ffmpeg_path'] = '/path/ffmpeg'` – J_Scholz Apr 20 '22 at 13:57

0 Answers0