Skip to content
Sample of Mandelbrot Set (Escape-Time Fractal)

shiffman_pearson_textbook · mandelbrotSet

Mandelbrot Set (Escape-Time Fractal)

The Mandelbrot set rendered by escape-time iteration of z = z^2 + c on a window of the complex plane. For each pixel mapped to c = x + yi, iterate z starting from 0 until |z|^2 > escape_radius or max_iter is reached. Pixels in the set are coloured by in_set_color; escaped pixels are shaded by iteration count with the standard log-correction smooth shading. step(n) advances the camera along a deep-zoom trajectory: the centre is a circular orbit of period `orbit_period` around (center_re, center_im) with radius `orbit_radius`, and the zoom grows geometrically from zoom_start to zoom_end across the same period. Default settings render the static canonical view (orbit_radius=0, zoom_start=zoom_end=1.3).

Original: https://natureofcode.com/

▶ Try the live demo (JS port) ▶ Run reference.py via Pyodide 10 formulas have instant JS ports. The Pyodide path runs the actual Python in your browser — first load is ~6 MB, then instant.

Family
shiffman_pearson_textbook
Source
shiffman · pearson
Evolution
escape_time_fractal
Render
iteration_count_to_palette
Complexity
high
Agents
1
Year
1980
Author
Benoit Mandelbrot (1978/1982); Daniel Shiffman (Nature of Code Ch. 11 / Coding Train Mandelbrot challenges, 2012)

Math essence

Vectorised numpy: for each pixel, c = re + i*im; iterate z_re = z_re^2 - z_im^2 + c_re; z_im = 2*z_re*z_im + c_im until |z|^2 > escape_radius or max_iter. Iteration count drives palette t; escaped pixels get t = (iter + 1 - log(log|z^2|)/log 2) / max_iter, a continuous value used by the chosen palette (viridis, magma, ocean, rainbow). step(n) advances tick -> camera orbit + zoom -> re-render.

Parameters

Name Type Default Range Description
dim int 480 120–1200
max_iter int 220 32–800 Max iterations before a pixel is declared 'in the set'.
escape_radius float 2 1.5–4 |z| threshold for declaring escape.
center_re float -0.75 -2.5–1
center_im float 0 -1.5–1.5
zoom_start float 1.3 0.1–50
zoom_end float 1.3 0.1–200 Final zoom at end of orbit; equal to zoom_start = static.
orbit_radius float 0 0–1.5 0 = static centre; >0 = circular orbit.
orbit_period int 240 4–4000
orbit_phase float 0 0–1
palette_mode str viridis —
in_set_color tuple 8,6,18 —
seed int null 0–999999

Reference

First 60 lines of reference.py. Full source: library_external/shiffman_pearson_textbook/mandelbrotSet/reference.py

from __future__ import annotations

from typing import ClassVar

import numpy as np


class MandelbrotSet:
    """Mandelbrot set — escape-time fractal on the complex plane.

    For each pixel mapped to a complex number ``c = x + yi`` in a window of
    the complex plane, iterate ``z = z² + c`` starting from ``z = 0`` until
    either ``|z|² > escape_radius`` or ``max_iter`` iterations are reached.
    Pixels that never escape (the Mandelbrot set itself) are rendered as
    the ``in_set_color``; pixels that escape are shaded by the number of
    iterations (smoothed with the standard log-correction for a continuous
    gradient).

    Step(n) advances the centre and zoom along a slow Mandelbrot-zoom
    trajectory (frame n is centred at ``c0 * exp(i * 2π * orbit_angle * n / orbit_period)``),
    producing the textbook deep-zoom animation. The default ``c0 = -0.7885``
    is the famous 'seahorse valley' deep-zoom locus.
    """

    family: ClassVar[str] = "shiffman_pearson_textbook"
    sketch: ClassVar[str] = "mandelbrotSet"

    def __init__(
        self,
        dim: int = 480,
        max_iter: int = 220,
        escape_radius: float = 2.0,
        center_re: float = -0.75,
        center_im: float = 0.0,
        zoom_start: float = 1.3,
        zoom_end: float = 1.3,
        orbit_radius: float = 0.0,
        orbit_period: int = 240,
        orbit_phase: float = 0.0,
        palette_mode: str = "viridis",
        in_set_color: tuple[int, int, int] = (8, 6, 18),
        seed: int | None = None,
    ) -> None:
        self.dim = dim
        self.max_iter = max_iter
        self.escape_radius = escape_radius
        self.center_re = center_re
        self.center_im = center_im
        self.zoom_start = zoom_start
        self.zoom_end = zoom_end
        self.orbit_radius = orbit_radius
        self.orbit_period = orbit_period
        self.orbit_phase = orbit_phase
        self.palette_mode = palette_mode
        self.in_set_color = np.array(in_set_color, dtype=np.float32)
        self._seed = seed
        self.reset()

    def reset(self, seed: int | None = None) -> None:
        if seed is not None:

Try running it locally

The reference implementation is self-contained Python with numpy. Install the package and run:

python library_external/shiffman_pearson_textbook/mandelbrotSet/reference.py

Need help? See Learn for the standard environment setup, or Playground for an in-browser sketchbook.

More from shiffman · pearson

Sample of Genetic Algorithm (Evolve Toward Target String)

Genetic Algorithm (Evolve Toward Target String)

shiffman_pearson_textbook · geneticAlgorithmString

A textbook genetic algorithm that evolves a population of binary strings toward a target ASCII string (default 'HELLO WORLD!').

View →
Sample of Lindenmayer L-System (Textbook Formulation)

Lindenmayer L-System (Textbook Formulation)

shiffman_pearson_textbook · lindenmayerTextbook

Lindenmayer L-system string rewriting with a 2-D turtle interpreter, the canonical textbook formulation used in both Shiffman's Nature of Code Ch.

View →
Sample of Perlin Noise Flow Field

Perlin Noise Flow Field

shiffman_pearson_textbook · perlinFlowField

A 2-D scalar field of Perlin gradient noise is sampled on a coarse grid; each cell is assigned a heading theta = 2*pi * n.

View →
Sample of Random Walk / Drunkard's Walk

Random Walk / Drunkard's Walk

shiffman_pearson_textbook · randomWalkDrunkard

Many random walkers each start at the canvas center and take a stochastic 2-D step every tick.

View →
Sample of Reynolds Boids (Separation / Alignment / Cohesion)

Reynolds Boids (Separation / Alignment / Cohesion)

shiffman_pearson_textbook · boidsClassic

Reynolds' classic boids flocking model.

View →
Sample of Stochastic Recursive Fractal Tree

Stochastic Recursive Fractal Tree

shiffman_pearson_textbook · recursiveTree

Stack-based stochastic fractal tree from Shiffman's Nature of Code Ch.

View →

See all shiffman_pearson_textbook artworks →