1

im new in here and new in python, im doing some animations with animation.FuncAnimation of matplotliib. The animation works perfectly but i´m having problems saving the animations. here is the part of the code of the animation.

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

fig, ax = plt.subplots()

line,  = ax.plot(range(N),sin(x[0,:]),'o-')
ax.axis([0,1,-1,1])

def animate(i):
    line.set_ydata(sin(x[i,:]))  # update the data
    return line,

def init():
    line.set_ydata(np.ma.array(x[0,:], mask=True))
    return line,

ani = animation.FuncAnimation(fig, animate, np.arange(1, 10000),
    interval=25, init_func=init, blit=True)
ani.save('2osc.mp4', writer="ffmpeg")
plt.show()

where x[:,:] is previously set. ani.save is saving every frame of the animation as a .npg image instade of saving the movie. I dont know if this is how it is suposed to work and i have to do the movie with the .npg with another program or if im doing something wrong. Obs: i've previously installed ffmpeg and it seems to be working just fine.

2
  • Did you install the required libraries (sudo apt-get install ffmpeg)? Also, can you share the exact code that you run? Commented Jul 2, 2015 at 19:40
  • I did not install the required libraries but my code is working now, i closed everything, restarted pyhton and run it again and now is saving the movies just fine. Thanks anyway!! Commented Jul 3, 2015 at 18:43

1 Answer 1

2

For me this code seems to be running fine:

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

fig, ax = plt.subplots(1,1)
x=np.linspace(np.pi,4*np.pi,100)
N=len(x)
ax.set_xlim(len(x))
ax.set_ylim(-1.5,1.5)
line,  = ax.plot([],[],'o-')

def init():
    line.set_ydata(np.ma.array(x[:], mask=True))
    return line,

def animate(i, *args, **kwargs):
    y=np.sin(x*i)
    line.set_data(np.arange(N),y)            # update the data
    return line,

ani = animation.FuncAnimation(fig, animate, init_func=init, 
     frames=100, interval=10, blit= False, repeat = False)
ani.save('2osc.mp4', writer="ffmpeg")
fig.show()

You can install the ffmpeg library by using:

sudo apt-get install ffmpeg

Sign up to request clarification or add additional context in comments.

1 Comment

you were right, it is working fine, i had to close everything restart python and run the code again and now i have a lot of beautifull videos.^^ sorry if i make you lose your time. thks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.