0
import numpy as np
from matplotlib import pyplot as plt
from matplotlib import animation

def update_grid(data):
      global grid

      x = np.random.randint(N)
      y = np.random.randint(N)
      grid[x, y] += 1
      #new_grid = grid.copy()
      counter = 0
      while np.max(grid) >=4:
            x, y = np.where(grid >= 4)
            grid[x,y] = 0
            grid[x%N, (y-1)%N] += 1
            grid[x%N,(y+1)%N] += 1
            grid[(x-1)%N,y%N] += 1
            grid[(x+1)%N,y%N] += 1
            #At the border
            counter +=1

      mat.set_data(grid)
      print(grid)
      return [mat]

N = 10
grid = np.zeros((N, N))

fig, ax = plt.subplots()
mat  = ax.matshow(grid)
ani = animation.FuncAnimation(fig, update_grid,  interval = 1 ,save_count = 50,blit=True)
plt.show()

I am not sure as to why the plot is not updating at every interval and it keeps on giving me a blank.

JohanC
  • 71,591
  • 8
  • 33
  • 66
  • New contributor You didn't include the code that initialized the grid. To make the code work, I added the import and initialized with zeros. Now, what's probably happening is matplotlib not knowing the desired color ranges. You can add them via e.g. `ax.matshow(grid, vmin=0, vmax=3)`. Apart from that, the code seems to have a high chance of getting into an infinite loop. – JohanC Feb 27 '21 at 09:34
  • Awesome, Thanks JohanC it works now. Yes that's the goal of the code. It is to build a Sandpile model that is fully randomized. – Radical partical Feb 27 '21 at 11:33

0 Answers0