On this page
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 is in the Mandelbrot set when
stays bounded. The escape count is the first with , tested as . 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 the cardioid is and the disc is . Brent’s cycle test catches other interior points: a point whose orbit returns within 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 and gets the smooth value
A view is a centre (default ) and a radius (default 1.5), half the shorter side of the plane on screen. Pixel of a image is the point
Radii stay between and 4. Each redraw prints the view as -x -y -r -i options you can reuse.
How it works
session.pykeeps the view, a history of up to 200 views and the zoom rules.preview.pymoves and scales the old picture at once.render.pydraws a quarter-size picture first, then bands (four per CPU core) in worker processes, iterating all pixels at once with NumPy.gui.pyis the Tkinter window andframework.pythe 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 of periods and phase :
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
| Key | Action |
|---|---|
| Left / right click | Zoom in / out 10x, centred on the clicked point |
| Drag | Move the view; the picture follows the pointer at once |
| Shift + drag | Rubber band: zoom to the box |
| Wheel | Zoom 1.5x per notch about the pointer |
| Ctrl + left click | Move the centre of the view to the clicked point |
| Ctrl + right click / P | Redraw the same counts with a new random palette |
| Middle click / S | Save the image as output/<date>_<time>.png |
| Arrow keys | Move 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+Right | Previous / next view |
| Home / R | The first view |
| M | Smooth colours or bands of whole counts |
| C / K | Next palette / save the palette to a file |
| F / F1 or H / Q or Esc | Full screen / help / quit |
Command line
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| Option | Meaning | Default |
|---|---|---|
-i, --iterations | Maximum iterations per pixel | 200 |
-x, -y | Real and imaginary part of the centre | -0.75, 0 |
-r, --radius (also -m, --magnification) | Half of the shorter side of the view; smaller zooms in | 1.5 |
-wi, --width / -he, --height | Image size in pixels | 90% of the screen height, square |
-s, --save | Save every image that is drawn | off |
-o, --out | Folder for saved images | output |
-nm, --noMulti | Render in the main process only (in a thread) | off |
--workers | Number of worker processes | one per CPU |
--seed | Seed of the random palettes | random |
--palette | random, a preset name or a palette file | random |
--smooth / --no-smooth | Continuous colouring or bands | smooth |
--render FILE.png | Draw 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.




