我正在尝试使用 matplotlib 绘制 Mandelbrot 集。我已经用一种颜色完成了,但现在我试图根据迭代次数放入一些 cmap。所以,我的代码如下所示:
import random
import matplotlib.pyplot as plt
elem = 10000
x = []
y = []
colors = []
def iteration(x):
n = 0
result = 0
while n < 1000: # max iterations
result = pow(result, 2) + c
if abs(result) > 2:
return n
n += 1
return n
for i in range(elem):
c = complex(random.uniform(-2.5,1),random.uniform(-1.5,1.5))
x.append(c.real)
y.append(c.imag)
colors.append(iteration(c))
plt.scatter(x, y, ',', c = colors, cmap= 'magma', vmin = 0, vmax = 1000)
plt.axis('equal')
plt.axis([-2,1,-1.5,1.5])
plt.title('Mandelbrot Set', y = 1.05)
plt.ylabel('Im')
plt.xlabel('Re')
plt.colorbar()
plt.show()
我不确定如何在 plt.scatter 中定义 c、vmin 和 vmax。例如,如果 n 为 1000(最大迭代),我想要黑点,如果 n = 0,我想要另一种颜色。
请您参考如下方法:
我确实认为您的代码运行良好。只需要定义标记即可。
plt.scatter(x, y, marker=',', c = colors, cmap= 'magma', vmin = 0, vmax = 1000)
