以下是一个简单的绘制Mandelbrot集的Python代码:

import numpy as np
import matplotlib.pyplot as plt

def mandelbrot(c, max_iter):
    z = 0
    n = 0
    while abs(z) <= 2 and n < max_iter:
        z = z*z + c
        n += 1
    if n == max_iter:
        return 0
    else:
        return n

def mandelbrot_set(xmin, xmax, ymin, ymax, width, height, max_iter):
    x = np.linspace(xmin, xmax, width)
    y = np.linspace(ymin, ymax, height)
    pixels = np.zeros((height, width))
    for i in range(height):
        for j in range(width):
            pixels[i,j] = mandelbrot(x[j] + 1j*y[i], max_iter)
    return (x,y,pixels)

xmin, xmax, ymin, ymax = -2, 1, -1.5, 1.5
width, height = 500, 500
max_iter = 1000

x, y, pixels = mandelbrot_set(xmin, xmax, ymin, ymax, width, height, max_iter)

plt.figure(figsize=(10,10))
plt.imshow(pixels.T, cmap='hot', extent=[xmin, xmax, ymin, ymax])
plt.xlabel('Real')
plt.ylabel('Imaginary')
plt.show()

这个代码使用了NumPy和Matplotlib库。函数mandelbrot(c, max_iter)计算给定复数c对应的Mandelbrot集上的迭代次数,max_iter为最大迭代次数。函数mandelbrot_set(xmin, xmax, ymin, ymax, width, height, max_iter)生成给定范围和分辨率的Mandelbrot集,并返回网格中每个点的迭代次数。最后,代码使用Matplotlib绘制了Mandelbrot集的图像

帮我找绘制Mandelbrot集的python代码

原文地址: http://www.cveoy.top/t/topic/hoAM 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录