Python:根据类的无效 RGBA 参数 0.0 色点

标签 python matplotlib plot colors colormap

上下文

我有几点

 points = np.random.uniform(0,10, size = (10,2))
# array([[ 7.35906037,  6.50049804],
       [ 3.21883403,  3.81452312],
       [ 3.52107154,  1.68233797],
       [ 1.47699577,  6.01692348],
       [ 3.76051589,  0.25213394],
       [ 8.93701081,  5.20377479],
       [ 6.5347188 ,  2.12940006],
       [ 3.62550069,  5.80619507],
       [ 1.33393325,  5.0088937 ],
       [ 6.99034593,  7.40277623]])

并且它们被“分类”或标记。这意味着我有一个列表

    labels = np.random.randint(0,3, size = 10)
  # array([2, 0, 1, 2, 2, 1, 1, 0, 1, 2])

表示points中每个点的标签(按顺序)。

我还有一些加分

    extraPoints = np.random.uniform(0,10, size = (3,2))
# array([[ 1.91211141,  3.71208978],
#   [ 8.10463536,  1.88948511],
#   [ 9.79796593,  3.39432552]])

基本上这些点中的每一个都决定了类标签。它如何确定标签并不重要。但是您所需要知道的是,这些额外点中的每一个都与一个且仅一个标签相关联。所以有相同数量的 extraPoints 和标签可能性。

问题

我想做一个散点图。我想为 extraPoints 中的每个点分配不同的颜色,因此这种颜色将对应于每个类。这基本上意味着 extraPoints[0] 与类 0 关联,extraPoints[1] 与类 1 关联> 和 extraPoints[2] 与类 2 相关联。

此外,我想在points 中散点图。请记住,points 中的每个点都与 labels 中的相应标签相关联。 例如 [ 7.35906037, 6.50049804] 属于 2 类,因此具有与 extraPoints[2] = [ 9.79796593, 3.39432552] 相同的颜色。类似地,points 中的点 [ 3.21883403, 3.81452312]labels 中的类 0 相关联,因此具有相同的extraPoints[0] = [ 1.91211141, 3.71208978] 的颜色

我的尝试

我尝试在 plt.scatter() 中使用 c 参数,但我并不真正理解它是如何工作的,有时它有点工作,有时它说“无效的 RGBA 参数 0.0”,但似乎是任意的..

请注意,为了区分 pointsextraPoints,我将使 extraPoints 更大且更透明。

import matplotlib.pyplot as plt
# I scatter the points, and assign c to labels. So hopefully each
# point that ends up in the same label will have the same 
# color? I  think this part is fine, although I am not sure
plt.scatter(points[:,0], points[:,1], c = labels) 
plt.scatter(extraPoints[:,0], extraPoints[:,1], s = 100, alpha = 0.3, c = np.arange(len(extraPoints)))

您可以自己尝试,对于不同的执行(因为每次我们都有随机数组),我们可能会正确(或几乎)正确或得到标题中的错误。为什么会这样?

Extra -for the braves

鉴于这种情况,想象一下我也有一些值(value)观

    values = np.random.uniform(0,50, size = 3)
# array([ 14.63459424,  37.41573654,  34.45202082])

我的值的数量与我的标签类型和 extraPoints 的数量相同(在本例中为 3)。现在,每一个都与相应的 extraPoints 相关联。因此第一个 extraPoint 的第一个值等等..

我想做上面的图,但是颜色会有一个“渐变”,例如,值越小越亮,值越大越暗(或相反)。我怎样才能做到这一点?我阅读了有关颜色图的信息,但我无法将其与我的问题完全整合。

示例

例如对于上面的值,我们得到: scatter

如您所见,我无法控制颜色。不仅如此,我也不知道哪个点在哪个类中(除非我回去手动查看每个点,但显然我不希望这样)。这就是为什么(以及我不会在这里介绍的其他原因)我想根据 values 中的值为它们着色的原因。具体来说,我想说有一个值范围 [10, 20 30] 可以指导我的点的颜色,这样我就知道哪个类是“最强的”

最佳答案

第一个问题:代码无法运行,因为 np.random.uniform(0,10, size = 3) 给出了一个一维数组,而您稍后期望它是二维的(extraPoints[:,0])。

第二个问题:labels 可能有 1 到 3 个唯一条目,因此 np.unique(labels) 的长度可能为 1 到3(例如 labels 可能全为零,例如 np.unique(labels) == [0])这样你的点数比颜色数多。但是,c 需要单个颜色参数或与输入坐标长度相同的值列表。

第三个​​问题:如果提供长度为 3 或 4 的列表或数组,不清楚这应该是单个 RGB 或 RGBA 颜色还是颜色图的值列表。如果你真的遇到了这个问题,在你解决了第一个和第二个问题之前不能确定。

更新:前两个问题解决后,您可能只是在寻找颜色条和有用的颜色图。

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.colors

points = np.random.uniform(0,10, size = (10,2))

labels = np.random.randint(0,3, size = 10)

extraPoints = np.random.uniform(0,10, size = (3,2))

sc = plt.scatter(points[:,0], points[:,1], c = labels) 
sc2 = plt.scatter(extraPoints[:,0], extraPoints[:,1], s = 144, alpha = 0.7, 
            c = np.arange(len(extraPoints)))

plt.colorbar(sc)

plt.show()

enter image description here

或者,如果您想要单独的颜色:

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.colors

points = np.random.uniform(0,10, size = (10,2))

labels = np.random.randint(0,3, size = 10)

extraPoints = np.random.uniform(0,10, size = (3,2))

colors=["red", "gold", "limegreen"]
cmap = matplotlib.colors.ListedColormap(colors)

sc = plt.scatter(points[:,0], points[:,1], c = labels, cmap=cmap, vmin=-0.5,vmax=2.5 ) 
sc2 = plt.scatter(extraPoints[:,0], extraPoints[:,1], s = 144, alpha = 0.7, 
            c = np.arange(len(extraPoints)), cmap=cmap, vmin=-0.5,vmax=2.5)

plt.colorbar(sc, ticks=np.arange(len(extraPoints)))

plt.show()

enter image description here

关于Python:根据类的无效 RGBA 参数 0.0 色点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46094525/

相关文章:

python - 为什么 scipy.optimize.curve_fit 不适合数据?

matlab - 在图例中平方而不是线条 Matlab

python - 在图像上叠加 matplotlib quiver

python - 如何修复 MatPlotLib 的 mpl_finance 包?

plot - 带虚线的最大值图

python - 使用带有secondary_y的Seaborn + Pandas绘图时如何摆脱网格线

javascript - 基于每个节点的权重进行聚类的算法/函数?

python - 何时在正则表达式模式中使用原始字符串?

javascript - 在 JavaScript 中加载表单数据并在客户端运行某些内容

python - 有没有办法从python执行jq