How to plot the Mandelbrot set
Open Source Your Knowledge, Become a Contributor
Technology knowledge has to be shared and made accessible for free. Join the movement.
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:
Definition
The mandelbrot set is defined by the set of complex numbers for which the complex numbers of the sequence
z 0 = 0 z n + 1 = z n 2 + 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
A complex number (
The visual representation of the mandelbrot set may be created by determining, for each point
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
.
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
returnsMAX_ITER
, plot a black pixel, otherwise plot a pixel in a color that depends on the number of iterations returned bymandelbrot
This is called the "Escape time algorithm".
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.