【问题标题】:Python: Invalid RGBA argument 0.0 color points according to classPython:根据类无效的RGBA参数0.0色点
【发布时间】:2018-02-16 02:01:25
【问题描述】:

上下文

我有几点意见

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

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

勇敢者的额外奖励

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

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

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

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

示例

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

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

【问题讨论】:

    标签: python matplotlib plot colors colormap


    【解决方案1】:

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

    第二个问题labels 可能有 1 到 3 个唯一条目,因此 np.unique(labels) 的长度可能是 1 到 3(例如 labels 可能全为零,这样 @987654331 @) 这样你的点数就会多于颜色。但是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()
    

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

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

    【讨论】:

    • 第一个问题是错字,已更正。你是对的,应该是一个 3x2 数组
    • 第二个问题我可以使用c = np.arange(len(extraPoints))
    • 从而解决第二个问题,我们将(可能)有一些extraPoints 有颜色,但points 中没有任何点有那种颜色。这涵盖了这些情况。
    • 我想剩下的唯一问题是获取颜色图和颜色条?在这种情况下,请参阅更新的答案。
    • 我认为“弄乱颜色顺序”不足以描述问题。至少不是我理解的一个。我可以想象,此时最好停止讨论。您将对迄今为止所学到的关于颜色图和头脑中的c 参数的所有内容进行排序,然后提出一个新的明确问题,并带有minimal reproducible example 的问题。可能实际问题只是根据[2, 0, 1, 2, 2, 1, 1, 0, 1, 2][5,1,4] 映射到[4,5,1,4,4,1,1,5,1,4],但这也值得提出一个新问题,因为它与matplotlib 无关。
    【解决方案2】:

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

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

    我的插件可以满足我的需求

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

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

    【讨论】:

      猜你喜欢
      • 2018-04-01
      • 2018-11-20
      • 1970-01-01
      • 2015-03-13
      • 2015-11-26
      • 2015-04-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多