How to plot the Mandelbrot set

[CG]Maxime
2,098 views

Open Source Your Knowledge, Become a Contributor

Technology knowledge has to be shared and made accessible for free. Join the movement.

Create Content
Next: Adding some colors

Mandelbrot Set

The mandelbrot set is one of the most famous fractal, and it's very easy to draw. In this playground you will learn how to plot this:

Mandelbrot Set

Definition

The mandelbrot set is defined by the set of complex numbers c for which the complex numbers of the sequence zn remain bounded in absolute value. The sequence zn is defined by:

  • z0=0
  • zn+1=zn2+c

As a reminder, the modulus of a complex number is its distance to 0. In Python, this is obtained using abs(z) where z is a complex number. We assume that the sequence zn is not bounded if the modulus of one of its terms is greater than 2.

A complex number (x+iy) can be represented on a complex plane. The real part of the complex number is represented by a displacement along the x-axis and the imaginary part by a displacement along the y-axis.

The visual representation of the mandelbrot set may be created by determining, for each point c of a part of the complex plane, whether zn is bounded. The number of iterations to reach a modulus greater than 2 can be used to determine the color to use.

If still unclear, I recommend watching the great explanation of Dr Holly Krieger from MIT.

Computation of the Terms of the Sequence

Let's define the function mandelbrot that will return the number of iterations needed to reach a modulus greater than 2. If the number of iterations is greater than MAX_ITER, stop and return MAX_ITER.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
MAX_ITER = 80
def mandelbrot(c):
z = 0
n = 0
while abs(z) <= 2 and n < MAX_ITER:
z = z*z + c
n += 1
return n
if __name__ == "__main__":
for a in range(-10, 10, 5):
for b in range(-10, 10, 5):
c = complex(a / 10, b / 10)
print(c, mandelbrot(c))
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Plot of the Mandelbrot Set

Plotting the mandelbrot set is relatively simple:

  • Iterate over all the pixels of your image
  • Convert the coordinate of the pixel into a complex number of the complex plane
  • Call the function mandelbrot
  • If mandelbrot returns MAX_ITER, plot a black pixel, otherwise plot a pixel in a color that depends on the number of iterations returned by mandelbrot

This is called the "Escape time algorithm".

Black and white image
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
palette = []
im = Image.new('RGB', (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
color = 255 - int(m * 255 / MAX_ITER)
# Plot the point
draw.point([x, y], (color, color, color))
im.save('output.png', 'PNG')
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
1
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Feel free to change the plot window by changing the variables RE_START, RE_END, IM_START and IM_END. You can also change the espace radius or the value of MAX_ITER.

In the next section, we will add some colors to our draw.

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