A basic implementation#

Now let’s iterate the interesting section of the complex plain and apply our formula.

To start off, let’s do this very explicitely.

Hide code cell content
%matplotlib widget
import time
import matplotlib.pyplot as plt
plt.rcParams['figure.dpi'] = 300
x_min, x_max = -2.5, 1.0
y_min, y_max = -1.25, 1.25
max_iterations = 100
resolution = 1000
tic = time.perf_counter()

x = [x_min + (x_max - x_min) / (resolution - 1) * index for index in range(resolution)]
y = [y_min + (y_max - y_min) / (resolution - 1) * index for index in range(resolution)]

iterations = []
for _y in y:
    row = []
    for _x in x:
        c = complex(_x, _y)
        c0 = c
        for iteration in range(max_iterations):
            c = c**2 + c0
            if abs(c) > 2.0:
                break
        row.append(iteration)
    iterations.append(row)
    
toc = time.perf_counter()
print(f"Calculating the mandelbrot set took {toc-tic:.1f} seconds.")
Calculating the mandelbrot set took 4.7 seconds.

By tracking the number of iterations in a 2D structure, we’re able be visualize the results as an image to finally render the Mandelbrot Set.

Hide code cell source
fig, axes = plt.subplots()
axes.imshow(iterations[::-1], extent=(x_min, x_max, y_min, y_max), cmap="RdGy")
plt.tight_layout()