How to make an animation and how to output as a html file

You need to download the ffmpeg package.
Type "conda install -c menpo ffmpeg".
You need to put the command " plt.rcParams['animation.html'] = 'html5' ".

In [1]:
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation

#dpi=80
#fig = plt.figure(figsize=(400/dpi,200/dpi),dpi=dpi)
fig = plt.figure()

x = np.arange(0, 10, 0.1)

ims = []
for a in range(50):
    y = np.sin(x - a)
    im = plt.plot(x, y, "b")
    ims.append(im)
        
plt.rcParams['animation.html'] = 'html5'
ani = animation.ArtistAnimation(fig, ims)
plt.close()
In [2]:
ani
Out[2]:

For overplotted graph,

In [3]:
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation

fig = plt.figure(figsize=(5,2),dpi=160)
#fig = plt.figure(figsize=(10,5))
x = np.arange(0, 10, 0.1)

ims = []
for a in range(50):
    y = np.sin(x - a)
    im1 = plt.plot(x, y, "b")
    im2 = plt.plot(x, y**2, "b")
    im3 = plt.plot(x, y**3, "b")
    ims.append(im1+im2+im3)
        
plt.rcParams['animation.html'] = 'html5'
ani = animation.ArtistAnimation(fig, ims)
plt.close()
In [4]:
ani
Out[4]:
In [5]:
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation

fig = plt.figure()
x = np.arange(0, 10, 0.1)
ax1=fig.add_subplot(111)
ax2=ax1.twinx()
ims = []
for a in range(10):
    #ax1=fig.add_subplot(111)
    im1 = ax1.plot(x, np.sin(x-a), "b")
    #ax2=ax1.twinx()
    im2 = ax2.plot(x, np.cos(x-a), "r")
    ims.append(im1+im2)
    #plt.show()
        
plt.rcParams['animation.html'] = 'html5'
ani = animation.ArtistAnimation(fig, ims)
plt.close()
In [6]:
ani
Out[6]:

You need to put brackets [] like "ims.append([im])" when you make an animation of colormaps.

In [7]:
import numpy as np
import matplotlib.pyplot as plt

fig = plt.figure(figsize=(3,3),dpi=160)
#fig = plt.figure()

N = 50
x1 = np.linspace(-5, 5, N)
x2 = np.linspace(-5, 5, N)

X1, X2 = np.meshgrid(x1, x2)

ims = []
for it in range(100):
    Z = np.exp(-(X1**2+X2**2)*(it+1)*0.01)
    im=plt.imshow(Z,origin='lower')
    
    ims.append([im])
    
plt.rcParams['animation.html'] = 'html5'    
ani = animation.ArtistAnimation(fig, ims)
plt.close()
In [8]:
ani
Out[8]:

For subplots, you need to put comma(,) after the instance name of panels.

In [9]:
n = 300
x = np.random.randn(n)
y1 = np.exp(x) + np.random.randn(n)
y2 = np.exp(x) * 3 + np.random.randn(n)

fig = plt.figure()
ims=[]
for it in range(100):
    plt.subplot(121)
    ax1, =plt.plot(x, y1*np.sin(x*0.1*it), "k.")
    plt.subplot(122)
    ax2, =plt.plot(x, y2*np.cos(x*0.1*it), "k.")
    ims.append([ax1, ax2])
    #plt.show()
    
plt.rcParams['animation.html'] = 'html5'
ani = animation.ArtistAnimation(fig, ims)
plt.close()
In [10]:
ani
Out[10]:

We can use "FuncAnimation" method.

In [11]:
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
import numpy as np

fig = plt.figure(figsize=(6,3),dpi=160)
#fig=plt.figure()
ax = fig.add_subplot(111)
x = np.arange(10)
ax.set_xlim(0, 9)
ax.set_ylim(0, 1)

def update_anim(i):
    ax.cla()
    y = np.random.rand(1000)
    ax.hist(y,bins=100)

plt.rcParams['animation.html'] = 'html5'
anim = FuncAnimation(fig, update_anim, blit=False,frames=100)
plt.close()
In [12]:
anim
Out[12]:
In [13]:
import numpy as np
import matplotlib.animation as animation

fig, ((ax1, ax2), (ax3, ax4)) = plt.subplots(2, 2,figsize=())

def updateData(curr):
    for ax in (ax1, ax2, ax3, ax4):
        ax.clear()
    x1 = np.random.normal(-2.5, 1, 10000)
    x2 = np.random.gamma(2, 1.5, 10000)
    x3 = np.random.exponential(2, 10000)+7
    x4 = np.random.uniform(14,20, 10000)
        
    ax1.hist(x1, normed=True, bins=np.linspace(-6,1, num=21), alpha=0.5)
    ax2.hist(x2, normed=True, bins=np.linspace(0,15,num=21), alpha=0.5)
    ax3.hist(x3, normed=True, bins=np.linspace(7,20,num=21), alpha=0.5)
    ax4.hist(x4, normed=True, bins=np.linspace(14,20,num=21), alpha=0.5)

anim = animation.FuncAnimation(fig, updateData, repeat=False,frames=50)
plt.close()
In [14]:
%%time
anim
CPU times: user 0 ns, sys: 0 ns, total: 0 ns
Wall time: 14.3 µs
Out[14]:
In [15]:
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
import numpy as np

fig=plt.figure()
ax = fig.add_subplot(111)
x1 = np.linspace(-5, 5, N)
x2 = np.linspace(-5, 5, N)

X1, X2 = np.meshgrid(x1, x2)
ax.set_xlim(0, 9)
ax.set_ylim(0, 1)

def update_anim(it):
    ax.cla()
    Z = np.exp(-(X1**2+X2**2)*(it+1)*0.01)
    ax.imshow(Z,origin='lower')

plt.rcParams['animation.html'] = 'html5'
anim = FuncAnimation(fig, update_anim, blit=False,frames=100)
plt.close()
In [16]:
anim
Out[16]:

Another way

At the first, you save the animation as a gif file.
By using a IPython.display package 'HTML', show the gif on the notebook.
Output this notebook by following the next commands: File->Download as->HTML Embebbed

In [17]:
ani.save('sample.gif', writer='imagemagick')
In [18]:
from IPython.display import HTML
HTML('<img src="sample.gif">')
Out[18]: