title: Matplotlib FuncAnimation slow
body: <p>I have created a simple animation of a surface binding process using matplotlib's FuncAnimation. The result however is very slow. I suspect it is because I am redrawing all elements at each frame, but I haven't figured out a work-around. Any help appreciated.</p>
  
  <pre><code>import matplotlib
  matplotlib.use('TKAgg')      # import proper graphics back-end for Mac OS X
  
  
  import numpy as np
  from matplotlib import pyplot as plt
  from matplotlib import animation
  from matplotlib.collections import PatchCollection
  from random import *
  
  nx = 20     # x size of lattice
  ny = 20     # y size of lattice
  
  pAds = 0.01     # adsorption probability per time step
  pDes = 0.0075   # desorption probability per time step
  
  tMax = 500     # number of time steps
  
  surface = np.zeros((nx,ny))              # create surface
  xc = [0]
  yc = [0]
  
  
  # initialization and time step of simulation
  
  def init():
      # initialize an empty list of circles
      patches = []            # empty array to hold drawable objects
      for x in range(0,nx):
          for y in range(0,ny):
              if(surface[x][y] == 0):
                  patches.append(ax_surf.add_patch(plt.Circle((x+0.5,y+0.5),0.45,color='w')))
      lines, = ax_covr.plot([],[])
      patches.append(lines)
      return patches
  
  def animate(i): 
      patches = []            # empty array of circles to be drawn
      for x in range(0,nx):
          for y in range(0,ny):
              if(surface[x][y] == 0):
                  if(random() &lt; pAds):
                      surface[x][y] = 1
                      patches.append(ax_surf.add_patch(plt.Circle((x+0.5,y+0.5),0.45,color='b')))
                  else:
                      patches.append(ax_surf.add_patch(plt.Circle((x+0.5,y+0.5),0.45,color='w')))
              else:
                  if(random()&lt;pDes):
                      surface[x][y] = 0
                      patches.append(ax_surf.add_patch(plt.Circle((x+0.5,y+0.5),0.45,color='w')))
                  else:
                      patches.append(ax_surf.add_patch(plt.Circle((x+0.5,y+0.5),0.45,color='b')))
      coverage = np.sum(surface)/(nx*ny)
      xc.append(i)
      yc.append(coverage)
      lines, = ax_covr.plot(xc,yc,'ro',ms=2,lw=0)
      patches.append(lines)
      return patches
  
  # set up figure and animate
  
  
  fig = plt.figure()
  ax_surf = plt.subplot2grid((1, 2), (0, 0))
  ax_covr = plt.subplot2grid((1, 2), (0, 1))
  ax_surf.set_xlim(0,nx)
  ax_surf.set_ylim(0,ny)
  ax_covr.set_xlim(0,tMax)
  ax_covr.set_ylim(0,1)
  
  ax_surf.set_aspect(1)
  ax_surf.axis('off')
  
  ax_covr.set_aspect(tMax)
  
  ax_surf.hold(False)
  
  anim = animation.FuncAnimation(fig, animate, init_func=init, frames=tMax, interval=0, blit=True,repeat=False)
  plt.show()
  </code></pre>