Skip to main content

Make a movie with Python-2

Here is another way to make a movie using matplotlib

In [1]:
import matplotlib.pyplot as plt
from matplotlib import pyplot as plt, animation

I needed to make a movie of only two frames that are not generated in a plotting function so I found a trick to do that.

Let's say you have two data arrays, data1 and data2

In [ ]:
fig, ax  = plt.subplots(nrows=1, ncols=1,dpi=72)
def plot_loop(i):
 if i==0:
    img = ax.imshow(data1)
    ax.set_title('before')
 elif(i==1):
    img = ax.imshow(data2)
    ax.set_title('after')
 return img
    
In [ ]:
ani = animation.FuncAnimation(fig, plot_loop,frames=2, repeat=True)
In [ ]:
ani.save('movie.mp4')
plt.close(fig)