以下是绘制 Mandelbrot 集的 Python 代码:

import numpy as np
import matplotlib.pyplot as plt

# 设置图像大小和分辨率
fig = plt.figure(figsize=(16,12), dpi=100)

# 设置绘图区域
ax = fig.add_subplot(111)

# 设置坐标范围
xmin, xmax = -2.0, 1.0
ymin, ymax = -1.5, 1.5
ax.set_xlim([xmin, xmax])
ax.set_ylim([ymin, ymax])

# 设置迭代次数
max_iter = 100

# 设置颜色
color_map = plt.cm.hot

# 设置网格大小
nx, ny = 1000, 1000

# 生成网格
x = np.linspace(xmin, xmax, nx)
y = np.linspace(ymin, ymax, ny)
X, Y = np.meshgrid(x, y)

# 初始化矩阵
Z = np.zeros((ny, nx), dtype=np.complex)

# 迭代计算
for i in range(max_iter):
    Z = Z**2 + X + Y*1j

# 计算收敛速度
C = np.log2(abs(Z))

# 绘制图像
plt.imshow(C, cmap=color_map, extent=[xmin, xmax, ymin, ymax])

# 显示图像
plt.show()

运行上述代码,即可绘制出 Mandelbrot 集。

Python 绘制 Mandelbrot 集代码示例

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

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