Files
INF6B/simulations/mandelbrotset/wikipedia.py
T
2026-05-12 11:53:02 +02:00

41 lines
1.1 KiB
Python

import numpy as np
import matplotlib.pyplot as plt
# Setting parameters (these values can be changed)
x_domain, y_domain = np.linspace(-2, 2, 500), np.linspace(-2, 2, 500)
bound = 2
max_iterations = 50 # any positive integer value
colormap = "nipy_spectral" # set to any matplotlib valid colormap
func = lambda z, p, c: z**p + c
# Computing 2D array to represent the Mandelbrot set
iteration_array = []
for y in y_domain:
row = []
for x in x_domain:
z = 0
p = 2
c = complex(x, y)
for iteration_number in range(max_iterations):
if abs(z) >= bound:
row.append(iteration_number)
break
else:
try:
z = func(z, p, c)
except (ValueError, ZeroDivisionError):
z = c
else:
row.append(0)
iteration_array.append(row)
# Plotting the data
ax = plt.axes()
ax.set_aspect("equal")
graph = ax.pcolormesh(x_domain, y_domain, iteration_array, cmap=colormap)
plt.colorbar(graph)
plt.xlabel("Real-Axis")
plt.ylabel("Imaginary-Axis")
plt.show()