title: How to animate the colorbar in matplotlib
body: <p>I have an animation where the range of the data varies a lot. I would like to have a <code>colorbar</code> which tracks the max and the min of the data (i.e. I would like it not to be fixed). The question is how to do this.</p>
  
  <p>Ideally I would like the <code>colorbar</code> to be on its own axis.</p>
  
  <p>I have tried the following four things</p>
  
  <h2>1. Naive approach</h2>
  
  <p>The problem: A new colorbar is plottet for each frame</p>
  
  <pre><code>#!/usr/bin/env python
  """
  An animated image
  """
  import numpy as np
  import matplotlib.pyplot as plt
  import matplotlib.animation as animation
  
  fig = plt.figure()
  ax = fig.add_subplot(111)
  
  
  def f(x, y):
      return np.exp(x) + np.sin(y)
  
  x = np.linspace(0, 1, 120)
  y = np.linspace(0, 2 * np.pi, 100).reshape(-1, 1)
  
  frames = []
  
  for i in range(10):
      x       += 1
      curVals  = f(x, y)
      vmax     = np.max(curVals)
      vmin     = np.min(curVals)
      levels   = np.linspace(vmin, vmax, 200, endpoint = True)
      frame    = ax.contourf(curVals, vmax=vmax, vmin=vmin, levels=levels)
      cbar     = fig.colorbar(frame)
      frames.append(frame.collections)
  
  ani = animation.ArtistAnimation(fig, frames, blit=False)
  
  plt.show()
  </code></pre>
  
  <h2>2. Adding to the images</h2>
  
  <p>Changing the for loop above to</p>
  
  <pre><code>initFrame = ax.contourf(f(x,y)) 
  cbar      = fig.colorbar(initFrame)
  for i in range(10):
      x       += 1
      curVals  = f(x, y)
      vmax     = np.max(curVals)      
      vmin     = np.min(curVals)      
      levels   = np.linspace(vmin, vmax, 200, endpoint = True)
      frame    = ax.contourf(curVals, vmax=vmax, vmin=vmin, levels=levels)
      cbar.set_clim(vmin = vmin, vmax = vmax)
      cbar.draw_all()
      frames.append(frame.collections + [cbar])
  </code></pre>
  
  <p>The problem: This raises</p>
  
  <pre><code>AttributeError: 'Colorbar' object has no attribute 'set_visible'
  </code></pre>
  
  <h2>3. Plotting on its own axis</h2>
  
  <p>The problem: The <code>colorbar</code> is not updated.</p>
  
  <pre><code> #!/usr/bin/env python
   """
   An animated image
   """
   import numpy as np
   import matplotlib.pyplot as plt
   import matplotlib.animation as animation
  
   fig = plt.figure()
   ax1 = fig.add_subplot(121)
   ax2 = fig.add_subplot(122)
  
  
   def f(x, y):
       return np.exp(x) + np.sin(y)
  
   x = np.linspace(0, 1, 120)
   y = np.linspace(0, 2 * np.pi, 100).reshape(-1, 1)
  
   frames = []
  
   for i in range(10):
       x       += 1
       curVals  = f(x, y)
       vmax     = np.max(curVals)
       vmin     = np.min(curVals)
       levels   = np.linspace(vmin, vmax, 200, endpoint = True)
       frame    = ax1.contourf(curVals, vmax=vmax, vmin=vmin, levels=levels)
       cbar     = fig.colorbar(frame, cax=ax2) # Colorbar does not update
       frames.append(frame.collections)
  
   ani = animation.ArtistAnimation(fig, frames, blit=False)
  
   plt.show()
  </code></pre>
  
  <h2>A combination of 2. and 4.</h2>
  
  <p>The problem: The <code>colorbar</code> is constant.</p>
  
  <p>A similar question is posted <a href="https://stackoverflow.com/questions/31562176/matplotlib-animation-with-changing-colorbar-and-title-for-each-frame">here</a>, but it looks like the OP is satisfied with a fixed <code>colorbar</code>.</p>