简体   繁体   English

Matplotlib Python中的Cmap

[英]Cmap in matplotlib Python

I am trying to plot Mandelbrot set using matplotlib. 我试图使用matplotlib绘制Mandelbrot集。 I've done it with one color, but now i am trying to put in some cmap depending on number of iterations. 我用一种颜色完成了它,但现在我试图根据迭代次数输入一些cmap。 So, my code looks like: 所以,我的代码看起来像:

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()

I am not sure how to define c, vmin and vmax in plt.scatter. 我不知道如何在plt.scatter中定义c,vmin和vmax。 I would like to have, for example if n is 1000 (max iteration) black dot and another color if n = 0. 我想,例如,如果n是1000(最大迭代)黑点,如果n = 0则是另一种颜色。

I do think your code works very well. 我认为你的代码工作得很好。 Just need to define the marker. 只需要定义标记。

plt.scatter(x, y, marker=',', c = colors, cmap= 'magma', vmin = 0, vmax = 1000)

在此输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM