# How to plot the Mandelbrot set

## 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 c$c$ for which the complex numbers of the sequence zn${z}_{n}$ remain bounded in absolute value. The sequence zn${z}_{n}$ is defined by:

- z0=0${z}_{0}=0$
- zn+1=z2n+c${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 zn${z}_{n}$ is not bounded if the modulus of one of its terms is greater than 2.

A complex number (x+iy$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$c$ of a part of the complex plane, whether zn${z}_{n}$ 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.