python绘制朱兰螺旋
时间: 2024-08-20 19:02:28 浏览: 104
朱兰螺旋,也称为螺旋形或Sierpinski三角形,是一种分形几何图形。在Python中,我们可以使用matplotlib库来绘制这种图案。以下是一个简单的示例,展示如何用递归的方式创建朱兰螺旋:
```python
import matplotlib.pyplot as plt
import numpy as np
def julia_set(c, max_iterations=256, resolution=800):
x = np.linspace(-2, 2, resolution)
y = np.linspace(-1.5, 1.5, resolution)
X, Y = np.meshgrid(x, y)
Z = X + 1j * Y
C = complex(0, 0) # 初始复数点
image = np.zeros((resolution, resolution), dtype=np.uint8)
for i in range(resolution**2):
if max_iterations > 0:
z = Z[i]
for n in range(max_iterations):
z = z**2 + c
if abs(z) > 2: # 如果超出单位圆,则跳出循环
break
else: # 没有跳出循环,表示z未离开单位圆
image[i] = n % 256 # 将灰度值赋给像素
return image
# 定义朱兰序列中心的复数c
c_values = [complex(-0.745, -0.125), complex(-0.75, -0.13)]
fig, axs = plt.subplots(nrows=2, ncols=2, figsize=(8, 8))
for ax, c in zip(axs.flat, c_values):
ax.imshow(julia_set(c), cmap='hot', extent=(-2, 2, -1.5, 1.5), interpolation='bicubic')
ax.set_title(f"C = {c.real:.2f} + {c.imag:.2f}i")
ax.axis('off')
plt.tight_layout()
plt.show()
阅读全文