Search the documentation

Type to search. Use ↑ and ↓ to move, Enter to open.

Mandelbrot Set Explorer

A Python and Tkinter Mandelbrot explorer that renders with NumPy in worker processes, with box zoom, view history and random, preset or saved palettes.

Screenshot of Mandelbrot Set Explorer
Status
Builds and runs
Languages
Python
Build system
Python (pip)
License
MIT
Upstream
Original work in this repository
Show in graph

Explore

Mandelbrot set explorer

Zoom into the Mandelbrot set with smooth escape-time colours. Click a point to draw its orbit; the panel shows the Julia set under the pointer.

Loading the interactive explorer…This explorer needs JavaScript.

A Mandelbrot explorer in Python with Tkinter and NumPy. Worker processes draw each view, so the window stays responsive: a coarse picture appears at once and sharpens.

Sample render

The mathematics

A complex number cc is in the Mandelbrot set when

zn+1=zn2+c,z0=0z_{n+1} = z_n^2 + c, \qquad z_0 = 0

stays bounded. The escape count is the first nn with ∣zn∣>2|z_n| > 2, tested as x2+y2>4x^2 + y^2 > 4. A point still bounded after the iteration limit (200 by default) is drawn black.

Points inside the main cardioid and the period-2 disc never escape. With q=(x−14)2+y2q = (x - \tfrac14)^2 + y^2 the cardioid is q (q+x−14)≤14y2q\,(q + x - \tfrac14) \le \tfrac14 y^2 and the disc is (x+1)2+y2≤116(x + 1)^2 + y^2 \le \tfrac1{16}. Brent’s cycle test catches other interior points: a point whose orbit returns within 10−1410^{-14} of an earlier position is on an attracting cycle. Neither shortcut changes a count.

Whole counts make bands, so by default a point is iterated to ∣zn∣>256|z_n| > 256 and gets the smooth value

ν=n−log⁡2ln⁡∣zn∣.\nu = n - \log_2 \ln |z_n| .

A view is a centre x0+iy0x_0 + iy_0 (default −0.75+0i-0.75 + 0i) and a radius rr (default 1.5), half the shorter side of the plane on screen. Pixel (px,py)(p_x, p_y) of a w×hw \times h image is the point

c=x0+(px−w2)s+i(y0−(py−h2)s),s=2rmin⁡(w,h).c = x_0 + \Bigl(p_x - \tfrac w2\Bigr) s + i\left(y_0 - \Bigl(p_y - \tfrac h2\Bigr) s\right), \qquad s = \frac{2r}{\min(w, h)} .

Radii stay between 10−1310^{-13} and 4. Each redraw prints the view as -x -y -r -i options you can reuse.

How it works

  1. session.py keeps the view, a history of up to 200 views and the zoom rules.
  2. preview.py moves and scales the old picture at once.
  3. render.py draws a quarter-size picture first, then bands (four per CPU core) in worker processes, iterating all pixels at once with NumPy.
  4. gui.py is the Tkinter window and framework.py the command line.

Colouring

A palette has 257 entries: entry 0 is black (inside the set), entries 1 to 256 form a closed loop. A random palette is three cosines, one per RGB channel, with a random whole number mm of periods and phase φ\varphi:

channel(j)=1+round⁡(127(1+cos⁡(2πmj256+φ))),j=0,…,255.\text{channel}(j) = 1 + \operatorname{round}\Bigl(127\Bigl(1 + \cos\bigl(\tfrac{2\pi m j}{256} + \varphi\bigr)\Bigr)\Bigr), \qquad j = 0, \dots, 255 .

The presets are fire, ice, rainbow, grey and sunset. K saves a palette as JSON, {"format": "mandelbrot-palette", "colours": [[r, g, b], ...]} with 257 colours; --palette FILE or the Load palette menu reads it back.

Controls

KeyAction
Left / right clickZoom in / out 10x, centred on the clicked point
DragMove the view; the picture follows the pointer at once
Shift + dragRubber band: zoom to the box
WheelZoom 1.5x per notch about the pointer
Ctrl + left clickMove the centre of the view to the clicked point
Ctrl + right click / PRedraw the same counts with a new random palette
Middle click / SSave the image as output/<date>_<time>.png
Arrow keysMove the view by a tenth of its shorter side
+ / -Zoom in / out 2x about the centre
[ / ]Half / double the iteration limit
Backspace or Alt+Left, Alt+RightPrevious / next view
Home / RThe first view
MSmooth colours or bands of whole counts
C / KNext palette / save the palette to a file
F / F1 or H / Q or EscFull screen / help / quit

Command line

Bash
python3 -m venv .venv
.venv/bin/pip install -r requirements.txt
.venv/bin/python framework.py                                         # the whole set
.venv/bin/python framework.py -x -0.7436 -y 0.1318 -r 3E-4 -i 1000    # a spiral near the seahorse valley
.venv/bin/python framework.py --render spiral.png -wi 1600 -he 1000 --palette fire -x -0.7436 -y 0.1318 -r 3E-4 -i 1500
OptionMeaningDefault
-i, --iterationsMaximum iterations per pixel200
-x, -yReal and imaginary part of the centre-0.75, 0
-r, --radius (also -m, --magnification)Half of the shorter side of the view; smaller zooms in1.5
-wi, --width / -he, --heightImage size in pixels90% of the screen height, square
-s, --saveSave every image that is drawnoff
-o, --outFolder for saved imagesoutput
-nm, --noMultiRender in the main process only (in a thread)off
--workersNumber of worker processesone per CPU
--seedSeed of the random palettesrandom
--paletterandom, a preset name or a palette filerandom
--smooth / --no-smoothContinuous colouring or bandssmooth
--render FILE.pngDraw one image and exit, without a window or Tkinter

Wrong values print a message and exit with status 2. Python 3.10 or newer is needed; on Linux Tkinter comes from the system (sudo apt install python3-tk). Run the tests with .venv/bin/python -m pytest.

CPU time at 800 × 800 for the seahorse valley (radius 0.003, 1500 iterations): 1118 ms plain, 1283 ms with the cycle test. Around the period-3 bulb (radius 0.05, 3000 iterations): 15090 ms plain, 1101 ms with it.

Run it yourself

Build
cd Mandelbrot/Mandelbrot-set-explorer
sudo apt-get install -y python3-tk
python3 -m pip install numpy Pillow pytest
python3 -m venv .venv
.venv/bin/pip install -r requirements.txt
Run
python3 framework.py
# Smoke test without a display
timeout 20 xvfb-run -a python3 framework.py -wi 160 -he 120 -s
Test
.venv/bin/python -m pytest

From the repository root, the task runner wraps these commands:

mp.py
python3 scripts/mp.py deps mandelbrot-explorer
python3 scripts/mp.py ci mandelbrot-explorer
python3 scripts/mp.py run mandelbrot-explorer

Connections

Neighbourhood in the knowledge graph

Projects and concepts within two links of this page. Select a node to open it.

Mandelbrot Set ExplorerMandelbrot Fractal Generator (web)Mandelbrot ColoringMandelbrot setEscape-time algorithmColour mappingComplex numbersBuddhabrotChaos PatternsPySpaceColour spacesFractal geometryBuddhabrotBurning Ship fractalGeometry and computer graphicsNumber theory and algebraMonte Carlo methodsChaos EquationsChaos DotterIterated function systemsBarnsley fernChaos gameSierpinski triangleTinkerbell mapStrange attractorsChaos Theory Visuals
ProjectsConceptsDomainsShow in graph →