Issue
Going through some tutorials on matplotlib animations and encountered this problem. I am using the matplotlib.animation funcanimation as follows:
import matplotlib.animation as animation
import numpy as np
from pylab import *
def ani_frame():
fig = plt.figure()
ax = fig.add_subplot(111)
ax.get_xaxis().set_visible(False)
ax.get_yaxis().set_visible(False)
im = ax.imshow(rand(7, 7), cmap='gray', interpolation='nearest')
tight_layout()
def update_img(n):
print(n)
tmp = rand(7, 7)
im.set_data(tmp)
return im
ani = animation.FuncAnimation(fig, update_img, np.arange(0, 20, 1), interval=200)
writer = animation.writers['ffmpeg'](fps=5)
ani.save('demo.mp4', writer=writer)
return ani
ani_frame()
This generates the following output:
0 0 1 2 3 4 5
and so on. It is calling the first argument twice. How can I prevent this?
Solution
You can use an initialization function and provide it to FuncAnimation
using the init_func
argument. That way the first call will be on the init function and not the update function.
import matplotlib.animation as animation
import numpy as np
from pylab import *
def ani_frame():
fig = plt.figure()
ax = fig.add_subplot(111)
ax.get_xaxis().set_visible(False)
ax.get_yaxis().set_visible(False)
im = ax.imshow(rand(7, 7), cmap='gray', interpolation='nearest')
tight_layout()
def init():
#do nothing
pass
def update_img(n):
print(n)
tmp = rand(7, 7)
im.set_data(tmp)
ani = animation.FuncAnimation(fig, update_img, np.arange(0, 20, 1),
init_func=init, interval=200)
writer = animation.writers['ffmpeg'](fps=5)
ani.save('demo.mp4', writer=writer)
return ani
ani_frame()
This prints 0 1 2 3 ....
Answered By - ImportanceOfBeingErnest
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.