title: Python: Animating colormap of changing 2D numpy array
body: <p>I have a 2D numpy array (named enviro_grid) with zeros, ones, and twos that shuffle around per loop iteration.  I would like to animate the iterations of a changing colormap so that I can sort of visually verify/demonstrate that all the agents are following the behaviour I expect.  </p>
  
  <p>Skeleton outline of code:</p>
  
  <pre><code>import numpy as np
  import random
  import pylab as plt
  
  #...initialize values, setup, seed grid with 0's, 1's, 2's, etc...
  
  for t_weeks in range(time_limit):
  
      for j in range(player_n):
  
      #...Here lie a bunch of for/if loops which shuffle values around via rules...
      #...culminating in grid/array updates via the following line...
      #...which is seen a few times per iteration as the P[:,j]'s change.
      #...Note that P[6, j] and P[7, j] are just x, y array locations for each agent...
      #...while P[0, j] is just a designation of 1 or 2
  
          enviro_grid[int( P[6, j] ), int( P[7, j] )] = int( P[0, j] )
  
      #...Then I have this, which I don't really understand so much as...
      #... just copy/pasted from somewhere
  
      im = plt.imshow(enviro_grid, cmap = 'hot')
      plt.colorbar(im, orientation='horizontal')
      plt.show()
  </code></pre>
  
  <p>I've looked at a few links already for help; for instance, these</p>
  
  <p><a href="https://stackoverflow.com/questions/39472017/how-to-animate-the-colorbar-in-matplotlib">How to animate the colorbar in matplotlib</a></p>
  
  <p><a href="https://stackoverflow.com/questions/16657334/colormap-issue-using-animation-in-matplotlib">Colormap issue using animation in matplotlib</a></p>
  
  <p><a href="http://jakevdp.github.io/blog/2012/08/18/matplotlib-animation-tutorial/" rel="nofollow noreferrer">http://jakevdp.github.io/blog/2012/08/18/matplotlib-animation-tutorial/</a></p>
  
  <p>but I'm just too out of my league re:  Python to understand half of what I'm seeing, much less tailor it to my own code.  Just to help illustrate the kind of very basic help that I will probably need to be useful, the plotting I've done in the past is more of the form plot(x, y, options), and any animation I've done was just a natural byproduct of plotting during loop iterations.  All of this fig.method and plt.method stuff, culminating in a final plt.show(), confuses and enrages me. The use of def() functions in all of the above examples further exacerbates my lack of clarity as far as transitioning lines to my own context.  </p>
  
  <p>Would anyone mind providing a working solution for this based on my code?  I can provide more details if needed but am trying to keep this short per stackoverflow preference.</p>
  
  <p>Thanks in advance for any help anyone can provide.</p>