Fundamentals 8 min read

Bring Your Python Plots to Life: Game of Life Animation with Matplotlib

This tutorial shows how to use Matplotlib's animation module to create a Game of Life visualisation in Python, covering the rules, grid setup, code implementation, and how to generate and display the animated plot for data‑science applications.

Python Crawling & Data Mining
Python Crawling & Data Mining
Python Crawling & Data Mining
Bring Your Python Plots to Life: Game of Life Animation with Matplotlib

Using Matplotlib Animations

Matplotlib is the most widely used library for plotting and data visualisation in Python. Beyond static charts, it also supports simple animations, which can be demonstrated by implementing Conway's Game of Life.

Game of Life Rules

Start with an N×N grid (the example uses 50×50).

Randomly populate the grid with a number of "cells" (1500 cells chosen from 2500 possible positions).

A cell dies if it has ≤1 neighbour.

A cell dies if it has ≥4 neighbours.

A cell survives with 2 or 3 neighbours.

An empty cell becomes alive if it has exactly 3 neighbours.

Setting Up the Grid

First import the required libraries.

import time
from IPython import display
import matplotlib.pyplot as plt
import matplotlib.animation as animation

Define the input variables for the board.

# Input variables for the board
boardsize = 50  # board will be X by X where X = boardsize
pad = 2         # padded border, do not change this!
initial_cells = 1500  # number of initial cells to be placed

Generate random coordinates for the initial cells.

pos_list = []
for i in range(initial_cells):
    pos_list.append([random.randint(1, boardsize), random.randint(1, boardsize)])

Initialize the board as a NumPy array (52×52 because of the padding) and fill it using a helper function init_board() (implementation omitted, available on the linked GitHub).

# Initialize the board
my_board = np.zeros((boardsize + pad, boardsize + pad))
my_board = init_board(pos_list, my_board)

Creating the Animation

Enable notebook plotting and create a figure.

%matplotlib notebook
fig = plt.gcf()

Show the first frame.

im = plt.imshow(my_board)
plt.show()

Define the animation update function.

def animate(frame):
    im.set_data(update_board(my_board))
    return im,

Create the animation with FuncAnimation, specifying 200 frames and a 50 ms interval.

anim = animation.FuncAnimation(fig, animate, frames=200, interval=50)

The resulting animation (GIF) demonstrates the evolving Game of Life.

Game of Life animation
Game of Life animation

Conclusion

The same animation technique can be applied to many data‑science visualisations, such as illustrating Monte Carlo simulations, animating time‑series models, showing clustering evolution, or generating dynamic heatmaps.

GitHub: https://github.com/yiuhyuk/game_of_life Source: Towards Data Science article
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

animationPythonMatplotlibGame of Life
Python Crawling & Data Mining
Written by

Python Crawling & Data Mining

Life's short, I code in Python. This channel shares Python web crawling, data mining, analysis, processing, visualization, automated testing, DevOps, big data, AI, cloud computing, machine learning tools, resources, news, technical articles, tutorial videos and learning materials. Join us!

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.