Mandelbrot Pixel Art¶
By the end of this lab you'll be able to:
- Use complex number arithmetic in Python to test Mandelbrot membership
- Draw a raster image by placing colored dots on a grid using
dot() - Understand what "escape time" means and how it produces color gradients
A low-resolution pixel art version of the Mandelbrot set — one of the most famous mathematical objects ever discovered. Each pixel's color tells you how quickly the Mandelbrot iteration "escapes" to infinity, or (black) whether it stays bounded forever.
Welcome to the Mandelbrot Set!
The Mandelbrot set looks infinitely complex but is computed from a single rule:
z = z² + c. Apply it repeatedly and ask: does z escape or stay close to zero?
Let's code it together!
How It Works¶
For each pixel (px, py) in a grid, we compute a complex number c in the range [-2.5, 1] × [-1.25, 1.25]. Then we repeatedly apply z = z² + c starting from z = 0. If |z| > 2, the point has "escaped" — we color it by how many steps it took. If it never escapes after max_iter steps, it's in the Mandelbrot set and colored black.
Sample Code¶
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | |
What Do You Think Will Happen?
The Mandelbrot set is the black region — points that never escape.
Will it look like a rough blob, or a recognizable shape?
Make your guess — then click Run to find out! (This one is slow — be patient!)
Try It Now¶
Even at low resolution, the cardioid (heart-like) main body and the round bulge to its left are recognizable. Were you right?
How It Works¶
complex(cr, ci) creates a complex number c = cr + ci·i. The key iteration z = z * z + c computes z² using Python's built-in complex multiplication — no trigonometry needed! abs(z) gives √(real² + imag²), the distance from the origin.
Explanation Table¶
| Line | What it does |
|---|---|
cr = -2.5 + px * 3.5 / cols |
Map pixel column to real axis position |
ci = -1.25 + py * 2.5 / rows |
Map pixel row to imaginary axis position |
z = z * z + c |
The Mandelbrot iteration |
while abs(z) <= 2 |
Stop when z escapes radius 2 |
count |
Number of iterations = escape time = color |
Learning Check¶
Your Turn — Zoom Into the Bulge
Change the range to zoom in: cr = -1.5 + px * 0.5 / cols and ci = -0.5 + py * 1.0 / rows.
Predict what the zoomed region will look like — then run it to explore!
Zooming in reveals more detail in the boundary region — small bulges appear that look just like the main shape. This self-similarity at every zoom level is the defining property of a fractal!
Experiments¶
-
Increase resolution. Change
cols = 80androws = 66withpixel = 4. More detail, slower. You'll know it worked when the main body has sharper edges. -
More colors. Extend the
colorslist to 20 entries with a full rainbow. You'll know it worked when the boundary region shows more color bands. -
Increase max_iter. Change
max_iter = 50. Points near the boundary need more iterations to escape. You'll know it worked when the boundary detail improves. -
Try a different zoom. Use
cr = -0.75 + px * 0.02 / colsandci = -0.1 + py * 0.02 / rowsto zoom into the "seahorse valley". You'll know it worked when you see a miniature Mandelbrot shape.
Math Meets Art!
You computed one of the most famous mathematical objects in history using
z = z*z + c — just one line of code! The Mandelbrot set is infinitely complex
yet emerges from the simplest possible iteration.
Up next: Julia Set — the Mandelbrot's cousin.