The matplotlib call to produce an animation and save it in mp4 format is then simply:Īnim = animation.FuncAnimation(fig, animate, init_func=init, frames=100, blit=True)Īnim.save('animation_random.mp4', fps=10) # fps = FramesPerSecondĪ random initialization gives rise to the following animation, which ends in a configuration with 3 stable patterns ( block, blinker and a diamond-shaped structure that settles to a blinker in 8 steps) after approximately 50 iterations. We also need the functions init and animate the latter updates the content of the plot with evolved iterations of the universe. In the code above, we have set the parameters for the evolution as described by the original logic of the game, and we initialize a matplotlib figure using the matshow directive. Im = ax.matshow(init_universe(rows,cols),cmap=cm.binary) The code that follows is based on this very useful tutorial, which contains instructions to embed matplotlib animations directly in the ipython notebook. However, matplotlib also offers the possibility of generating animations and saving them directly in mp4 format. If n is the number of rows of the grid, the padboard has n+2 rows, which range from 0 to n+1, or, equivalently, from -1 to n! Visualization of Life and creation of mp4 videos with matplotlibįor the visualization of the evolution of our random universe we could create a series of png plots and stitch them together to produce an animated gif. Thus, we create a padboard with 2 columns and 2 rows more than the dimensions of the grid. Note that in the above code we make use of a wonderful property of arrays in python, namely that the last element of an array arr can be referenced either as arr or as arr. (neighbors > overcrowd or neighbors < underpop):Įlif not grid and neighbors = reproduction: Then we apply the evolution logic: cells die when underpopulated or overcrowded, and new cells are born when the reproduction condition (3 alive neighbors) is fulfilled: At every position i,j we compute the sum of all cells in positions and then we subtract the center point at i,j. In our implementation, we create a padding around the original universe, which allows us to define the neighbors in an easy way without having to worry whether a particular cell is at the border or not. The classical rules of the game of life set the parameters for overcrowding, under-population and reproduction as 3, 2, 3, respectively. Now, for the evolution logic, let us code a function that takes a universe as input, together with the parameters that regulate its evolution, and outputs the new universe after one iteration. The call to generate the figure, with black cells representing live (or 1) states, is as follows: A very simple way of initializing the grid to random values, allowing for variable grid dimensions, is as follows:Īn example of a random universe created with the init_universe(rows, cols) function with 600 cells distributed in 20 rows and 30 columns can be seen in the figure on the right. We will code a init function for the initialization of the grid and a evolve routine for the evolution of the universe. Our python implementation will use a two-dimensional numpy array to store the grid representing the universe, with values 1 for live and 0 for dead cells. Yet another python implementation of Life ![]() ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |