import numpy as np
import matplotlib.pyplot as plt
# Set the size of the image
n = 200
# Set the range of the x and y axes
x_min, x_max = -2, 1
y_min, y_max = -1.5, 1.5
# Generate the grid of points
x = np.linspace(x_min, x_max, n)
y = np.linspace(y_min, y_max, n)
X, Y = np.meshgrid(x, y)
Z = np.zeros_like(X)
# Set the initial value of Z
Z[0,:] = 0
Z[1,:] = 1
# Calculate the mandelbrot set
for i in range(1, 200):
Z_old = Z
Z = Z - Z_old**2 + Z_old*np.random.randn(n)
if abs(Z) > 2:
break
# Plot the set
fig = plt.figure()
ax = fig.add_subplot(111)
c = ax.imshow(Z, cmap='hot', extent=(x_min, x_max, y_min, y_max))
plt.show()
This script generates a grid of points in the complex plane and iteratively calculates the magnitude of each point. If the magnitude exceeds 2, the point is considered part of the Mandelbrot set and is plotted in the imshow function of matplotlib.
You can adjust the size of the image, the range of the x and y axes, and the number of iterations by changing the values of n, x_min, x_max, y_min, y_max, and i.