title: Animate scatter plot with colorbar using matplotlib
body: <p>I have spent a serious amount of time trying <strong>to animate</strong> scatter plot where the colour of the marker is defined by a number.</p>
  <p>Below is my attempt, which sort of works but not really as planned:</p>
  <ul>
  <li>After each animation step, the old points should be removed. Instead, the new points are simply added to the points already on plot.</li>
  <li>The colorbar should also be updated at each step according to the values (just like time text is).</li>
  </ul>
  <p>However, whatever I seem to do, produces a blank chart. Is this really the best I can do with python when it comes to animating scatter?</p>
  <pre><code>import matplotlib.pyplot as plt
  import matplotlib.animation as animation
  import numpy as np
  
  time_steps = 50
  N_nodes = 100
  
  positions = []
  solutions = []
  for i in range(time_steps):
      positions.append(np.random.rand(2, N_nodes))
      solutions.append(np.random.random(N_nodes))
  
  fig = plt.figure()
  marker_size = 1
  ax = fig.add_subplot(111, aspect='equal', autoscale_on=False, xlim=(0, 1), ylim=(0, 1))
  time_text = ax.text(0.02, 0.95, '', transform=ax.transAxes)
  
  def init():
      &quot;&quot;&quot; Initialize animation. &quot;&quot;&quot;
      scat = ax.scatter(positions[0][0], positions[0][1], s = marker_size, c = solutions[0], cmap = &quot;RdBu_r&quot;, marker = &quot;.&quot;, edgecolor = None)
      fig.colorbar(scat)
      time_text.set_text('Time step = %d' % 0)
  
      return scat, time_text
  
  def animate(i):
      &quot;&quot;&quot; Perform animation step. &quot;&quot;&quot;
      scat = ax.scatter(positions[i][0], positions[i][1], s = marker_size, c = solutions[i], cmap = &quot;RdBu_r&quot;, marker = &quot;.&quot;, edgecolor = None)
      time_text.set_text('Time step = %d' % i)
  
      return scat, time_text
  
  plt.xlabel('x [m]')
  plt.ylabel('y [m]')
  plt.grid(b=None)
  plt.show()
  ani = animation.FuncAnimation(fig, animate, interval=100, blit=True, repeat=True, init_func=init)
  
  ani.save('animation.gif', writer='imagemagick', fps = 8)
  </code></pre>