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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/46094525/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 17:28:32  来源:igfitidea点击:

Python: Invalid RGBA argument 0.0 color points according to class

pythonmatplotlibplotcolorscolormap

提问by Euler_Salter

Context

语境

I have some points

我有几点

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

and they are "classified" or labelled. This means that I have a list

它们被“分类”或贴上标签。这意味着我有一个列表

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

which represents the labels (in order) of each point in points.

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

I also have some extra points

我还有一些加分项

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

basically each of these points determines the class label. It doesn't matter HOW it determined the label. But all you have to know is that each of these extra points is associated to one and only one label. So there is an equal number of extraPoints and label possibilities.

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

problem

问题

I want to do a scatter plot. I want to assign a different color to each point in extraPoints, and hence this color will be the correspective to each class. This basically means that extraPoints[0]is associated with class 0, extraPoints[1]is associated with class 1and extraPoints[2]is associated with class 2.

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

Also, I want to scatter plot the points in points. Remember that each point in pointsis associated with the correspective label in labels. For instance [ 7.35906037, 6.50049804]is in class 2and thus has the same color of extraPoints[2] = [ 9.79796593, 3.39432552]. Similarly the point [ 3.21883403, 3.81452312]in pointsis associated with class 0in labelsand thus has the same color of extraPoints[0] = [ 1.91211141, 3.71208978]

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

My try

我的尝试

I tried using the cargument in plt.scatter()however I don't really understand how it works, and sometimes it sort of works, sometimes it says "Invalid RGBA argument 0.0" but seems to be arbitrary..

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

Notice that to distinguish pointsfrom extraPoints, I will make extraPointslarger and with more transparency.

注意区分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)))

As you can try out for yourself, for different executions (since every time we have random arrays) we might either get it right (or almost) or get the error in the title. Why does this happen?

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

Extra -for the braves

额外的 - 勇敢者

Given this context, imagine I had also some values

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

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

I have the same number of values as I have types of labels and extraPoints (i.e. 3 in this case). Now each of these is associated with the corresponding extraPoints. Thus the first value to the first extraPoint and so on..

我有相同数量的值,因为我有标签和 extraPoints 的类型(即在这种情况下为 3)。现在,这些中的每一个都与相应的 extraPoints 相关联。因此,第一个 extraPoint 的第一个值等等..

I would like to do the above plot, but the colors will have a "gradient" that becomes, for instance, lighter for smaller values and darker for larger values (or the opposite). How can I do that? I read about colormaps, but I can't quite integrate it with my problem.

我想做上面的图,但是颜色会有一个“渐变”,例如,较小的值会变亮,而较大的值(或相反)会变暗。我怎样才能做到这一点?我读过颜色图,但我无法将它与我的问题完全结合起来。

Example

例子

For instance for the values above, we obtain: scatter

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

As you can see, I have no control over the colors. Not only that, but I have no idea of which point is in which class (unless I go back and manually look at each point, but obviously I don't want this). This is why (and other reasons that I wont cover here) I want to color them based on a value in values. Specifically, I would like, say to have a range of values [10, 20 30]that can guide the color of my points, so that I know which class is "strongest"

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

采纳答案by ImportanceOfBeingErnest

First problem: the code does not run, since np.random.uniform(0,10, size = 3)gives a 1D array, while you later expect it to be 2D (extraPoints[:,0]).

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

Second problem: labelsmay have between 1 and 3 unique entries, hence np.unique(labels)may be of length 1 to 3 (e.g. labelsmay be all zeros, such that np.unique(labels) == [0]) such that you have more points than colors. However cexpects either a single color argument or a list of values of the same length than the input coordinates.

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

Third problem: If supplying a list or array of length 3 or 4, it is not clear whether this should be a single RGB or RGBA color or a list of values to colormap. If you actually run into this problem or not, cannot be said for sure until you have solved the first and second problem.

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

Update: after the first two problems are solved, you are probably just looking for a colorbar and a useful colormap.

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

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

在此处输入图片说明

Or, if you want to have individual colors:

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

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

在此处输入图片说明

回答by Euler_Salter

Thanks to ImportanceOfBeingErnest I managed to solve the problem. I know my explanation was really bad, but here I post it for someone who might find the same problem in the future:

感谢ImportanceOfBeingErnest,我设法解决了这个问题。我知道我的解释真的很糟糕,但在这里我将其发布给将来可能会发现相同问题的人:

ImportanceOfBeingErnest solution

ImportanceOfBeingErnest 解决方案

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

My add-in that does what I want

我的加载项可以满足我的要求

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))
# CREATE VALUES
values = np.random.uniform(0,50, size=3)

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

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

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

plt.show()

The difference is that the colors in scare now determined by the values in valuesin the same order of the labels, and at the same time, the points in extraPointsare being colored with the strength and order of values in values.

不同之处在于 中的颜色sc现在由 中的值以values与 相同的顺序确定labels,同时, 中的点extraPoints正在以 中值的强度和顺序进行着色values