Please consider this small script:
···
============================================================
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import animation
from time import time
def init():
return ax.cla()
def animate(i):
global t
r = np.random.random(10)
c = np.sin(2 * np.pi * r) * np.vstack(np.cos(2 * np.pi * r))
print(time() - t)
t = time()
return ax.contourf(c)
if __name__ == '__main__':
t = time()
fig = plt.figure()
ax = fig.add_subplot(111)
anim = animation.FuncAnimation(fig, animate, init_func=init,
frames=100,
interval=15,
blit=True)
anim.save('test.mp4', fps=15, extra_args=['-vcodec', 'libx264'])
The output from this script is as follows:
============================================================
0.21799993515
0.375
0.28200006485
0.31200003624
0.26500010490
0.31299996376
0.35899996757
0.32800006866
0.34399986267
0.375
0.40600013732
0.39100003242
0.42200016975
0.46899986267
0.5
0.51600003242
0.5
0.54699993133
0.57800006866
0.57799983024
0.59400010108
0.60899996757
0.64100003242
0.65599989891
0.73399996757
0.71900010108
0.73399996757
0.76600003242
0.79699993133
0.81200003624
1.04699993134
0.86000013351
0.89100003242
0.90600013732
0.92199993133
0.92199993133
1.0
1.10899996758
1.01600003242
1.03099989891
1.06200003624
1.11000013351
1.45299983025
1.21900010109
1.34299993515
1.31299996376
1.35900020599
1.39099979401
1.3900001049
1.43799996376
1.42199993134
1.45300006866
1.48399996758
1.5
1.5
1.54700016975
1.54699993134
1.625
1.625
1.625
1.65700006485
1.68700003624
1.67199993134
1.75
1.73399996758
1.73399996758
1.78200006485
1.81200003624
1.82800006866
1.8599998951
1.84299993515
1.93700003624
1.93799996376
1.98399996758
1.96900010109
2.04699993134
2.03099989891
2.03100013733
2.06299996376
2.06200003624
2.10899996758
2.125
2.17199993134
2.14100003242
2.18700003624
2.21900010109
2.2349998951
2.2650001049
2.28100013733
2.28099989891
2.29600000381
2.32899999619
2.375
2.35899996758
2.42199993134
2.46900010109
2.46799993515
2.48500013351
2.5
2.51600003242
So every loops is slower and slower. Now, my original script is more complicated, and when it reaches 100th frame the single loop takes half a minute to finish compared to half a second after the first loop. Can someone suggest what is wrong with the above code, and how to avoid this time aggregation on every loop?
Thanks