title: Python matplotlib - updating data during animation in Conway's Game of Life
body: <p>The code below creates an animation for Conway's Game of Life using Python and matplotlib.</p>
  
  <p>I am not sure why I have to do:</p>
  
  <pre><code>grid = newGrid.copy()
  mat.set_data(grid)
  </code></pre>
  
  <p>Instead of simply:</p>
  
  <pre><code>mat.set_data(newGrid)
  </code></pre>
  
  <p>How can I update the array associated with the plot, without the above copying?</p>
  
  <pre><code>import numpy as np
  import matplotlib.pyplot as plt 
  import matplotlib.animation as animation
  
  N = 100
  ON = 255
  OFF = 0
  vals = [ON, OFF]
  
  # populate grid with random on/off - more off than on
  grid = np.random.choice(vals, N*N, p=[0.2, 0.8]).reshape(N, N)
  
  def update(data):
    global grid
    newGrid = grid.copy()
    for i in range(N):
      for j in range(N):
        total = (grid[i, (j-1)%N] + grid[i, (j+1)%N] + 
                 grid[(i-1)%N, j] + grid[(i+1)%N, j] + 
                 grid[(i-1)%N, (j-1)%N] + grid[(i-1)%N, (j+1)%N] + 
                 grid[(i+1)%N, (j-1)%N] + grid[(i+1)%N, (j+1)%N])/255
  
        if grid[i, j]  == ON:
          if (total &lt; 2) or (total &gt; 3):
            newGrid[i, j] = OFF
        else:
          if total == 3:
            newGrid[i, j] = ON
  
    grid = newGrid.copy()
    mat.set_data(grid)
    return mat 
  
  fig, ax = plt.subplots()
  mat = ax.matshow(grid)
  ani = animation.FuncAnimation(fig, update, interval=50,
                                save_count=50)
  plt.show()
  </code></pre>
  
  <p>The output seems correct - I can see gliders, and other expected patterns:</p>
  
  <p><img src="https://i.stack.imgur.com/28FWK.png" alt="Conway&#39;s Game of Life using Python/matplotlib"></p>