Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FuncAnimation.save() only saving 1 frame #5822

Closed
rlabbe opened this issue Jan 9, 2016 · 9 comments
Closed

FuncAnimation.save() only saving 1 frame #5822

rlabbe opened this issue Jan 9, 2016 · 9 comments

Comments

@rlabbe
Copy link

rlabbe commented Jan 9, 2016

Summary: matplotlib 1.5 only saving 1 frame of animation, 1.4.3 saves all frames

I have code that reads like this (link to full code below):

anim = animation.FuncAnimation(fig, bar_animate,
                               frames=100, interval=75)
anim.save('02_no_info.gif', writer='imagemagick')

This has always worked for me until I reran it a few days ago. Now only saves the first frame of the animations. I put prints in the bar_animate function, and it is being called 100 times. I stepped into the save routine: it builds a list generator - when I expand it out it contains only (1,), so only one frame gets saved. I didn't dig deep enough to figure out why that tuple was generated.

My environment is Anaconda 64-bit, Windows 7, Python 3.5.1, IPython 4.0.1, matplotlib 1.5.

If I downgrade matplotlib to 1.4.3 then the save works - all 100 frames are saved to the gif. If I then upgrade, only 1 frame is saved. I did this cycle a couple of times to verify that this is the only difference. The directories for both have a valid matplotlibrc as far as I can tell - I edited them to use imagemagick, and put the full path to imagemagick in, as required for windows.

The code that evokes this behavior lives here:
https://github.com/rlabbe/Kalman-and-Bayesian-Filters-in-Python/blob/master/animations/discrete_bayes_animations.ipynb

I'm a programmer, so if you can't reproduce this and need help tracking the bug down let me know.

@jenshnielsen
Copy link
Member

This is a duplicate of #5399 will be fixed in 1.5.1

@jenshnielsen
Copy link
Member

As a work around you can supply it with an empty init function

@rlabbe
Copy link
Author

rlabbe commented Jan 9, 2016

Wow, fast response, thanks!

Just tested, and this works.

@wendycrumrine
Copy link

wendycrumrine commented Jun 1, 2016

Hello! I am having the exact same problem, but when I attempted to supply it with an empty init function, it gave me an error. I am new to this, and perhaps interpreted it wrong. Anyways, would love any suggestions... My code is below. The animation on its own works fine, however it is only saving the first frame.

vORTmovie = np.zeros((len(xVEC),len(yVEC),np.int(len(tVEC)/50)))
for i in range(len(vORTstore[0,0,:])-1):
    if np.mod(i,50) == 0:
        vORTmovie[:,:,i/50] = vORTstore[:,:,i]

Writer = animation.writers['ffmpeg']
writer = Writer(fps=15, metadata=dict(artist='Me'), bitrate=1800) 

def data(i,vORTmovie,line):
    ax.clear()
    ax.set_xlabel(r'$x$')
    ax.set_ylabel(r'$y$')
    ax.set_zlabel(r'$vorticity$')
    set_elev = 50
    set_az = 340
    ax.view_init(elev=set_elev, azim=set_az)        
    ax.dist = 10
    ax.set_zlim(-4.5,4.5)
    line = ax.plot_surface(X,Y,vORTmovie[:,:,i],cmap=plt.cm.pink,rstride=1,cstride=1,linewidth=.00001,antialiased=True)
    return line,


fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.set_xlabel(r'$x$')
ax.set_ylabel(r'$y$')
ax.set_zlabel(r'$vorticity$')
set_elev = 50
set_az = 340
ax.view_init(elev=set_elev, azim=set_az)        
ax.dist = 10
ax.set_zlim(-4.5,4.5)
line = ax.plot_surface(X,Y,vORTmovie[:,:,0],cmap=plt.cm.pink,rstride=1,cstride=1,linewidth=.00001,antialiased=True)

ani = animation.FuncAnimation(fig, data,np.arange(1,len(vORTmovie[0,0,:])),  fargs=(vORTmovie, line), interval=1)
ani.save('ThreeVortices.mp4', writer=writer)

plt.show()

@jenshnielsen
Copy link
Member

@wendycrumrine Which version are you using This has been fixed in 1.5.1 so the easies solution is to upgrade to 1.5.1 if you can upgrade

@wendycrumrine
Copy link

#Import Tools
import numpy as np
import scipy.integrate as integrate
import matplotlib.pyplot as plt
from numpy import linalg as lin
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.animation as animation

#Create 2D meshgrid in Cartesian Space
xL = 2*np.pi
xNUMsteps = 40
deltaX = xL/xNUMsteps
xVEC = np.linspace(0,xL,xNUMsteps+1)
xVEC = xVEC[0:-1]

yL = 2*np.pi
yNUMsteps =40
deltaY = yL/yNUMsteps
yVEC = np.linspace(0,yL,yNUMsteps+1)
yVEC = yVEC[0:-1]

X,Y = np.complex_(np.meshgrid(xVEC,yVEC))

#Create 2D meshgrid in Fourier Space
deltaK_x = (2_np.pi)/xL
xKvec_1 = np.arange(0,(xNUMsteps+1)/2)
xKvec_2 = -1_xKvec_1[::-1][1:-1]
xKvec = deltaK_x*(np.concatenate((xKvec_1,xKvec_2)))

deltaK_y = (2_np.pi)/yL
yKvec_1 = np.arange(0,(yNUMsteps+1)/2)
yKvec_2 = -1_yKvec_1[::-1][1:-1]
yKvec = deltaK_y*(np.concatenate((yKvec_1,yKvec_2)))

yK,xK = np.complex_(np.meshgrid(yKvec,xKvec))

#Construct K2
K2 = (xK * xK) + (yK * yK)
K2[0,0] = (10 ** 64)

#Discretize Time
time = 13.5*np.pi
tNUMsteps = 1200
tDelta = time/tNUMsteps
tVEC = np.complex_(np.linspace(0,time,tNUMsteps+1))

#Initialize two circular vorticies
Gamma1 = 2.5_np.pi
Gamma2 = 2.5_np.pi
Gamma3 = 2.5_np.pi
vortexONE = np.exp( (-20_(X-xL/2+xL/7)*2) - (20(Y-yL/1.7)**2) )
vortexTWO = np.exp( (-20_(X-xL/2-xL/7)2) - (20(Y-yL/1.7)*2) )
vortexTHREE = np.exp( (-12
(X-xL/2)**2) - (12
(Y-yL/1.7 + yL/4)_*2) )
vORT = Gamma1 * vortexONE + Gamma2 * vortexTWO + Gamma3 * vortexTHREE

##Compute all initial terms of PDE
kinVisc = .0025
visc1 = np.exp(-1 * kinVisc * K2 * tDelta)
visc2 = np.exp(-1 * kinVisc * K2 * tDelta /2)

vORTstore = np.complex_(np.zeros((len(xVEC),len(yVEC),len(tVEC))))
vORTstore[:,:,0] = vORT
vORT_f = np.fft.fft2(vORT)
eta_f_OLD = np.zeros((len(X),len(Y)))

#Integrate vorticity through time
for i in range(len(tVEC)-1):
psi_f = vORT_f/K2
vX = np.fft.ifft2(1j * yK * psi_f)
vY = np.fft.ifft2(-1j * xK * psi_f)
gradVortX = np.fft.ifft2(1j * xK * vORT_f)
gradVortY = np.fft.ifft2(1j * yK * vORT_f)
eta_f = np.fft.fft2(-vX_gradVortX-vY_gradVortY)
if i == 0:
eta_f_OLD = eta_f
vORT_f = (vORT_f * visc1) + ((tDelta/2)_(3_eta_f - eta_f_OLD)*visc2)
eta_f_OLD = eta_f
vORTstore[:,:,i+1] = np.fft.ifft2(vORT_f)

#Animate
vORTmovie = np.zeros((len(xVEC),len(yVEC),np.int(len(tVEC)/50)))
for i in range(len(vORTstore[0,0,:])-1):
if np.mod(i,50) == 0:
vORTmovie[:,:,i/50] = vORTstore[:,:,i]

Writer = animation.writers['ffmpeg']
writer = Writer(fps=15, metadata=dict(artist='Me'), bitrate=1800)

def data(i,vORTmovie,line):
ax.clear()
ax.set_xlabel(r'$x$')
ax.set_ylabel(r'$y$')
ax.set_zlabel(r'$vorticity$')
set_elev = 50
set_az = 340
ax.view_init(elev=set_elev, azim=set_az)
ax.dist = 10
ax.set_zlim(-4.5,4.5)
line = ax.plot_surface(X,Y,vORTmovie[:,:,i],cmap=plt.cm.pink,rstride=1,cstride=1,linewidth=.00001,antialiased=True)
return line,

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.set_xlabel(r'$x$')
ax.set_ylabel(r'$y$')
ax.set_zlabel(r'$vorticity$')
set_elev = 50
set_az = 340
ax.view_init(elev=set_elev, azim=set_az)
ax.dist = 10
ax.set_zlim(-4.5,4.5)
line = ax.plot_surface(X,Y,vORTmovie[:,:,0],cmap=plt.cm.pink,rstride=1,cstride=1,linewidth=.00001,antialiased=True)

ani = animation.FuncAnimation(fig, data,np.arange(1,len(vORTmovie[0,0,:])), fargs=(vORTmovie, line), interval=1)
ani.save('ThreeVortices.mp4', writer=writer)

plt.show()

@wendycrumrine
Copy link

Thanks for the speedy reply! I'm not sure which version I'm using, ugh! I am using Spyder 2.3.8, Python 3.5.1 64bits, Qt 4.8.7, PyQt4 (API v2) 4.11.4 on Darwin.

I am assuming I am using an old version, and will attempt to upgrade through Anaconda now. :)

@jenshnielsen
Copy link
Member

You can get the version from something like

In [1]: import matplotlib
In [2]: matplotlib.__version__
Out[2]: '1.5.1+1967.g7d66623'

@wendycrumrine
Copy link

Thank you!

--wendy

On Wed, Jun 1, 2016 at 9:29 AM, Jens Hedegaard Nielsen <
notifications@github.com> wrote:

You can get the version from something like

In [1]: import matplotlib
In [2]: matplotlib.version
Out[2]: '1.5.1+1967.g7d66623'


You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
#5822 (comment),
or mute the thread
https://github.com/notifications/unsubscribe/ASx4KN_1xOmB5iAoVct8lvNLpY1QFP7jks5qHbNwgaJpZM4HBtvN
.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants