I've been having a hard time searching for answers on how to animate the evolution of an array in matplotlib. I have a 2D array which acts as a forest, where trees grow with a probability p, and catch fire with probability f. Then, each burning tree can burn its eight neighbours, if there are trees within those, obviosuly. A tree is represented with the value 1, a burning one with -1, and an empty cell with 0. I want to assign 3 different colors to represent the forest and animate its evolution. How should i do this? I've got two functions that simulate the fire propagation, which is where I suspect I should place the animation code:
def helper_func(forest):
for i in range(1, len(forest)-1):
for j in range(1, len(forest[i])-1):
if forest[i,j] == 1:
for h, v in neighbours:
if forest[i+h, j+v] == -1:
forest[i,j] = -1
return forest
where neighbours is a list of tuples which contain the x,y coordinates of the 8 neighbours. Also, i'm not going through the borders of the forest during iteration to prevent index errors while going through the neighbours (they are also not included while generating trees).
def propagation(forest):
copy = forest.copy()
while copy.all != helper_func(woods).all:
copy = helper_func(forest)
helper_func(forest)
return forest