Exploring the Sierpinski Carpet: A Python Visualization Guide
Written on
Chapter 1: Introduction to Fractals
Fractals are intriguing mathematical structures that have drawn interest from scientists, mathematicians, and artists. Among the most recognized fractals is the Sierpinski Carpet, a self-similar design made up of progressively smaller iterations of itself. This fractal emerges through a recursive process where the middle third of each square's side is removed, and this procedure is applied to the remaining squares. In this article, we will delve into how to visualize the Sierpinski Carpet using Python's matplotlib library. Additionally, we'll create an animation that illustrates the gradual formation of this fractal. Let’s embark on this journey to appreciate the elegance of the Sierpinski Carpet!
Section 1.1: Recursive Construction of the Carpet
As highlighted earlier, the code to generate the carpet utilizes recursion. At each level, we invoke the same function to construct the carpet one level deeper. Below is the code that produces the carpet for a specified depth, represented as a numpy array filled with ones for white and zeros for black:
import numpy as np
import matplotlib.pyplot as plt
def sierpinski_carpet(depth):
if depth == 0:
return np.ones((1, 1))
carpet = sierpinski_carpet(depth - 1)
center = np.zeros_like(carpet)
squares = np.block([[carpet, carpet, carpet],
[carpet, center, carpet],
[carpet, carpet, carpet]])
return squares
fig, ax = plt.subplots(figsize=(10, 10))
ax.set_aspect('equal')
ax.set_axis_off()
carpet = sierpinski_carpet(depth=5)
plt.imshow(carpet, cmap='binary')
plt.show()
Subsection 1.1.1: Adding Animation to Our Visualization
Now, let’s incorporate an animation to visualize the step-by-step construction of the carpet. We will utilize matplotlib's FuncAnimation feature for this purpose:
from matplotlib.animation import FuncAnimation
%matplotlib notebook
# Set up the initial plot
fig, ax = plt.subplots(figsize=(10, 10))
ax.set_aspect('equal')
ax.set_axis_off()
carpet = sierpinski_carpet(depth=1)
im = ax.imshow(carpet, cmap='binary')
# Define the update function for the animation
def update(depth):
carpet = sierpinski_carpet(depth)
im.set_data(carpet)
return [im]
# Create the animation object and display it
ani = FuncAnimation(fig, update, frames=range(1, 8), interval=1000)
plt.show()
The first video titled "Sierpinski Python Code Explained" provides a thorough breakdown of the code used to create the Sierpinski Carpet, elaborating on its recursive nature and functionality.
Section 1.2: The Beauty of Fractals
To conclude, the Sierpinski Carpet stands out as a captivating fractal pattern, composed of increasingly smaller replicas of itself. It maintains a self-similar form, appearing identical at any level of magnification, thus exemplifying the intricate beauty of mathematics. Through the power of Python and matplotlib, we can effectively visualize this fractal and even animate its development.
Chapter 2: Visualizing the Sierpinski Triangle
The second video, "Drawing a Sierpinski Triangle with Python Turtle," demonstrates how to create the Sierpinski Triangle using Python's Turtle graphics, offering a different approach to visualizing this classic fractal.