Matplotlib.Pyplot不会在散布中更改颜色

时间:2020-04-03 14:40:26

标签: matplotlib

我不明白为什么pyplot总是打印红色,但是变量却不断变化

def showDot(dot):
    classColormap = ListedColormap(['#FF0000', '#00FF00', '#000000'])
    pl.scatter(dot[0][0],dot[0][1],c=dot[1],cmap=classColormap)
    pl.show()

另外,当我写c = 2(恒定颜色,但不是红色)时,pyplot打印红色

1 个答案:

答案 0 :(得分:1)

对我来说,要使最终结果看起来像什么还不是很清楚,但是在我看来c必须是一些指定强度的数字。在我看来,c=dot[1]会在您的代码中返回一个数组。以下代码会产生不同的颜色。在这里,我根据距origo的距离简单地定义了强度。你可能还有别的东西。

我以前从未使用过颜色图,但是我猜想它需要对一系列值进行操作才能分配不同的颜色。如果加上一个和一个点,我猜是它总是从颜色图中分配相同的颜色(也许是中间值?)

from matplotlib import pyplot as pl
from matplotlib import colors as cl
import numpy as np

mean = (1, 2)
cov = [[1, 0.2], [.2, 1]]
dots = np.random.multivariate_normal(mean, cov, (2, 20))
# The distance from origo, which will define the intensity
dots_intensity = np.hypot(dots[0,:], dots[1,:])

classColormap = cl.ListedColormap(['#FF0000', '#00FF00', '#000000'])

pl.scatter(dots[0,:], dots[1,:],c=dots_intensity,cmap=classColormap)

希望有帮助。

我发现这篇文章很有用https://medium.com/better-programming/how-to-use-colormaps-with-matplotlib-to-create-colorful-plots-in-python-969b5a892f0c

enter image description here