Cmap in matplotlib Python
我正在尝试使用matplotlib绘制Mandelbrot集。我已经用一种颜色完成了此操作,但是现在我尝试根据迭代次数添加一些cmap。因此,我的代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | 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,则为另一种颜色。
我确实认为您的代码效果很好。只需定义标记即可。
1 | plt.scatter(x, y, marker=',', c = colors, cmap= 'magma', vmin = 0, vmax = 1000) |