Here is a Python program to picture the Mandelbrot set using matplotlib:
import numpy as np
import matplotlib.pyplot as plt
# Set up grid
x_min, x_max = -2, 1
y_min, y_max = -1, 1
width, height = 500, 500
x_values = np.linspace(x_min, x_max, width)
y_values = np.linspace(y_min, y_max, height)
x, y = np.meshgrid(x_values, y_values)
# Initialize output image
mandelbrot = np.zeros((width, height))
# Iterate and compute Mandelbrot set
for i in range(width):
for j in range(height):
c = complex(x[i,j], y[i,j])
z = c
for k in range(100):
if abs(z) > 2:
break
z = z*z + c
mandelbrot[i,j] = k
# Plot
plt.imshow(mandelbrot.T, extent=[x_min, x_max, y_min, y_max])
plt.title("Mandelbrot set")
plt.savefig("mandelbrot.png")
plt.show()
This plots the Mandelbrot set on a 500x500 grid spanning -2 to 1 on the x-axis and -1 to 1 on the y-axis. It iterates up to 100 times per pixel to determine if the complex number is diverging to infinity or staying bounded. The output is visualized using matplotlib's imshow().
Let me know if you have any other questions!