# 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 $$ remain bounded in absolute value. The sequence $$ is defined by:

- $$
- $$

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 $$ is not bounded if the modulus of one of its terms is greater than 2.

A complex number ($$) 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 $$ of a part of the complex plane, whether $$ 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`

.

### 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".

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.