我正在尝试使用以下代码来显示曼德布洛特集:
import numpy as np
import matplotlib.pyplot as plt
plt.rcParams['toolbar'] = 'None'
def mandelbrot(c, max_iter):
z = 0
for n in range(max_iter):
if abs(z) > 2:
return n
z = z*z + c
return max_iter
def mandelbrot_set(xmin, xmax, ymin, ymax, width, height, max_iter):
r1 = np.linspace(xmin, xmax, width)
r2 = np.linspace(ymin, ymax, height)
n3 = np.empty((width, height))
for i in range(width):
for j in range(height):
n3[i, j] = mandelbrot(r1[i] + 1j*r2[j], max_iter)
return n3.T
# Settings
xmin, xmax, ymin, ymax = -2.0, 1.0, -1.5, 1.5
width, height = 800, 800
max_iter = 256
# Generate Mandelbrot set
mandelbrot_image = mandelbrot_set(xmin, xmax, ymin, ymax, width, height, max_iter)
# Window
fig = plt.figure(figsize=(5, 5))
fig.canvas.manager.set_window_title('Mandelbrot Set')
ax = fig.add_axes([0, 0, 1, 1]) # Fill the whole window
ax.set_axis_off()
# Show fractal
ax.imshow(mandelbrot_image, extent=(xmin, xmax, ymin, ymax), cmap='hot')
plt.show()
我该如何持续放大分形,而不占用太多资源?我使用的是一台中端笔记本电脑,目前生成分形需要很长时间。在实现缩放功能时,有没有更快的方法来实现呢?