0

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
  • how about [FuncAnimation()](https://matplotlib.org/api/_as_gen/matplotlib.animation.FuncAnimation.html) in matplotlib ? – furas Jul 01 '20 at 00:24
  • maybe you need [heatmap](https://matplotlib.org/3.2.1/gallery/images_contours_and_fields/image_annotated_heatmap.html) to draw single frame of animation using colored rectangles. And run it in loop or with `FuncAnimation` to create animation). – furas Jul 01 '20 at 00:32
  • BTW: as for me it is not good idea to set new values in the same array which you use to get old values - this way you can calculate new values using other new values instead of old values. You should rather create new empty array and put new values in this array - and also copy values which doesn't change. – furas Jul 01 '20 at 00:34

0 Answers0