Gallery · Visualization

Fractal postcard

The Mandelbrot set and a Julia set in a few lines of vectorized NumPy, with smooth escape-time shading. Generative art, baked by a real run.

fractal-postcard.verso Python · C# · 5 cells Read-only preview Download

Fractal postcard

The Mandelbrot set in about twenty lines of vectorized NumPy: iterate z → z² + c and color each pixel by how quickly it escapes. Smooth shading keeps the bands from posterizing. NumPy ships with matplotlib, and Verso offers to install both the first time a cell imports them.

[1]
codePython
1import numpy as np
2import matplotlib.pyplot as plt
3
4width, height, max_iter = 900, 600, 120
5x = np.linspace(-2.2, 0.8, width)
6y = np.linspace(-1.0, 1.0, height)
7c = x[np.newaxis, :] + 1j * y[:, np.newaxis]
8
9z = np.zeros_like(c)
10escape = np.full(c.shape, max_iter, dtype=float)
11mask = np.ones(c.shape, dtype=bool)
12for i in range(max_iter):
13 z[mask] = z[mask] ** 2 + c[mask]
14 escaped = mask & (np.abs(z) > 2.0)
15 # smooth (fractional) escape count avoids hard color bands
16 escape[escaped] = i + 1 - np.log2(np.log(np.abs(z[escaped])))
17 mask &= ~escaped
18
19fig, ax = plt.subplots(figsize=(7.5, 5.0))
20ax.imshow(escape, cmap="magma", extent=[-2.2, 0.8, -1.0, 1.0])
21ax.set_axis_off()
22plt.tight_layout()
23plt.show()
✓completed

A postcard from a Julia set

Same iteration, one twist: hold c constant and vary the starting point instead. The constant below is shared with the notebook, so the caption under the image is written by a different language than the one that drew it.

[2]
codePython
1import numpy as np
2import matplotlib.pyplot as plt
3
4c_re, c_im = -0.7269, 0.1889
5width, height, max_iter = 900, 600, 160
6
7x = np.linspace(-1.6, 1.6, width)
8y = np.linspace(-1.1, 1.1, height)
9z = x[np.newaxis, :] + 1j * y[:, np.newaxis]
10c = complex(c_re, c_im)
11
12escape = np.full(z.shape, max_iter, dtype=float)
13mask = np.ones(z.shape, dtype=bool)
14for i in range(max_iter):
15 z[mask] = z[mask] ** 2 + c
16 escaped = mask & (np.abs(z) > 2.0)
17 escape[escaped] = i + 1 - np.log2(np.log(np.abs(z[escaped])))
18 mask &= ~escaped
19
20julia_c = f"{c_re} + {c_im}i"
21
22fig, ax = plt.subplots(figsize=(7.5, 5.2))
23ax.imshow(escape, cmap="twilight_shifted", extent=[-1.6, 1.6, -1.1, 1.1])
24ax.set_axis_off()
25plt.tight_layout()
26plt.show()
✓completed
[3]
codeC#
1$"Julia constant c = {Variables.Get<string>("julia_c")}: tweak it and the whole structure reshapes."
✓completed
Julia constant c = -0.7269 + 0.1889i: tweak it and the whole structure reshapes.

Run it yourself

Nothing on this page is executable; the outputs were baked in by a real run. Download the notebook and it runs anywhere Verso does: VS Code, the browser, or the terminal.

dotnet tool install --global Verso.Cli
verso serve fractal-postcard.verso

What this shows

The Mandelbrot set and a Julia set in a few lines of vectorized NumPy, with smooth escape-time shading. Generative art, baked by a real run.

Every language in this notebook reads and writes the same variable store, so a value produced in one cell is simply there in the next, whatever the language.