# How to plot the Mandelbrot set

[CG]Maxime
3,906 views

## Adding colors to the Mandelbrot Set

In order to add some colors, one could associate a color for each possible value of iterations.

In the following example, we are switching from RGB colors to HSV (hue, saturation, value) colors. This allows us to change the color easily by modifying only the hue.

Use of HSV colors
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
28
29
30
31
from PIL import Image, ImageDraw
from mandelbrot import mandelbrot, MAX_ITER
# Image size (pixels)
WIDTH = 600
HEIGHT = 400
# Plot window
RE_START = -2
RE_END = 1
IM_START = -1
IM_END = 1
im = Image.new('HSV', (WIDTH, HEIGHT), (0, 0, 0))
draw = ImageDraw.Draw(im)
for x in range(0, WIDTH):
for y in range(0, HEIGHT):
# Convert pixel coordinate to complex number
c = complex(RE_START + (x / WIDTH) * (RE_END - RE_START),
IM_START + (y / HEIGHT) * (IM_END - IM_START))
# Compute the number of iterations
m = mandelbrot(c)
# The color depends on the number of iterations
hue = int(255 * m / MAX_ITER)
saturation = 255
value = 255 if m < MAX_ITER else 0
# Plot the point
draw.point([x, y], (hue, saturation, value))
im.convert('RGB').save('output.png', 'PNG')
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
1
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

### Smooth coloring

In the previous example, you can see bands of color. To remove these bands, we can use a fractional escape count, also known as "normalized iteration count". The theory is a bit difficult, if you are interested in the mathematics, you should read this article entitled "Renormalizing the Mandelbrot Escape".

The mandelbrot function must be modified to add to the result 1 - log(log2(abs(z))) where z is the last computed value of the sequence (abs(z) > 2).

Smooth coloring
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
from math import log, log2
MAX_ITER = 80
def mandelbrot(c):
z = 0
n = 0
while abs(z) <= 2 and n < MAX_ITER:
z = z*z + c
n += 1
if n == MAX_ITER:
return MAX_ITER
return n + 1 - log(log2(abs(z)))
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
1
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

You can also increase the value of MAX_ITER to improve the rendering.

### Histogram coloring

In the previous codes, the colors are not equaly distributed. That's particularly true when MAX_ITER is high. To improve the situation, the number of pixels for each iteration number is counted up. For each iteration number, a color will be associated by giving a wider range of colors for iteration numbers that concerns more pixels.

Histogram and smooth coloring
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
from PIL import Image, ImageDraw
from mandelbrot import mandelbrot, MAX_ITER
from collections import defaultdict
from math import floor, ceil
def linear_interpolation(color1, color2, t):
return color1 * (1 - t) + color2 * t
# Image size (pixels)
WIDTH = 600
HEIGHT = 400
# Plot window
RE_START = -2
RE_END = 1
IM_START = -1
IM_END = 1
histogram = defaultdict(lambda: 0)
values = {}
for x in range(0, WIDTH):
for y in range(0, HEIGHT):
# Convert pixel coordinate to complex number
c = complex(RE_START + (x / WIDTH) * (RE_END - RE_START),
IM_START + (y / HEIGHT) * (IM_END - IM_START))
# Compute the number of iterations
m = mandelbrot(c)
values[(x, y)] = m
if m < MAX_ITER:
histogram[floor(m)] += 1
total = sum(histogram.values())
hues = []
h = 0
for i in range(MAX_ITER):
h += histogram[i] / total
hues.append(h)
hues.append(h)
im = Image.new('HSV', (WIDTH, HEIGHT), (0, 0, 0))
draw = ImageDraw.Draw(im)
for x in range(0, WIDTH):
for y in range(0, HEIGHT):
m = values[(x, y)]
# The color depends on the number of iterations
hue = 255 - int(255 * linear_interpolation(hues[floor(m)], hues[ceil(m)], m % 1))
saturation = 255
value = 255 if m < MAX_ITER else 0
# Plot the point
draw.point([x, y], (hue, saturation, value))
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
1
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

And that's all for the Mandelbrot Set! And if you like the Mandelbrot Set, you'll love the Julia Sets!

Open Source Your Knowledge: become a Contributor and help others learn.