Iterating the whole 2D-array#
Show code cell content
%matplotlib widget
import matplotlib.pyplot as plt
plt.rcParams['figure.dpi'] = 200
x_min, x_max = -2.5, 1.0
y_min, y_max = -1.25, 1.25
max_iterations = 100
resolution = 1000
We generate an array of complex numbers.
import numpy as np
x = np.linspace(x_min, x_max, resolution)
y = np.linspace(y_min, y_max, resolution)
c = x + y[:, None] * 1j
The inner function is modified to take the complex array
binary masking helps to just work on the necessary subset.
def mandelbrot_set(c, max_iterations):
c0 = c.copy()
iterations = np.zeros_like(c, dtype=np.uint)
for index in range(max_iterations):
mask = np.abs(c) < 2.0
c[mask] = c[mask]**2 + c0[mask]
iterations[mask] += 1
return iterations
iterations = mandelbrot_set(c, max_iterations)
Show code cell content
fig, axes = plt.subplots()
axes.imshow(iterations[::-1], extent=(x_min, x_max, y_min, y_max), cmap="RdGy")
plt.tight_layout()