【问题标题】:Changing colors from clusters for each class更改每个类的集群颜色
【发布时间】:2020-11-01 10:02:49
【问题描述】:

我有一个散点图,我正在绘制 14 个集群,但每 2 个集群属于同一类,它们以 14 种不同的颜色绘制,但我只需要 2 种颜色。因此,对于 14 个集群,我将只有 2 个不同的类,因为它们将成对分组。我的集群在我的数据框中从 A(1) 枚举到 N(14)。我想要做的是通过它们的颜色来识别每个集群,例如每个奇数得到蓝色,每个偶数得到红色。或者如果你按字母顺序:A 变蓝,B 变红,C 变蓝,D 变红,E 变蓝等等。

Link for the Data Frame

import pandas as pd
import numpy as np
from matplotlib import pyplot as plt

y = [0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0, 0,  0,  0,  0,  0,  0,
  0,  0,  0,  0, 0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
  0,  0,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,
  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,
  1,  1,  1,  1,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,
  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,
  2,  2,  2,  2,  2,  2,  3,  3,  3,  3,  3,  3,  3,  3,  3,  3,  3,  3,  3,  3,  3,  3,  3,  3,
  3,  3,  3,  3,  3,  3,  3,  3,  3,  3,  3,  3,  3,  3,  3,  3,  3,  3,  3,  3,  3,  3,  3,  3,
  3,  3,  3,  3,  3,  3,  3,  3,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,
  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,
  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  5,  5,  5,  5,  5,  5,  5,  5,  5,  5,  5,  5,  5,  5,
  5,  5,  5,  5,  5,  5,  5,  5,  5,  5,  5,  5,  5,  5,  5,  5,  5,  5,  5,  5,  5,  5,  5,  5,
  5,  5,  5,  5,  5,  5,  5,  5,  5,  5,  5,  5,  6,  6,  6,  6,  6,  6,  6,  6,  6,  6,  6,  6,
  6,  6,  6,  6,  6,  6,  6,  6,  6,  6,  6,  6,  6,  6,  6,  6,  6,  6,  6,  6,  6,  6,  6,  6,
  6,  6,  6,  6,  6,  6,  6,  6,  6,  6,  6,  6,  6,  6,  7,  7,  7,  7,  7,  7,  7,  7,  7,  7,
  7,  7,  7,  7,  7,  7,  7,  7,  7,  7,  7,  7,  7,  7,  7,  7,  7,  7,  7,  7,  7,  7,  7,  7,
  7,  7,  7,  7,  7,  7,  7,  7,  7,  7,  7,  7,  7,  7,  7,  7,  8,  8,  8,  8,  8,  8,  8,  8,
  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,
  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  9,  9,  9,  9,  9,  9,
  9,  9,  9,  9,  9,  9,  9,  9,  9,  9,  9,  9,  9,  9,  9,  9,  9,  9,  9,  9,  9,  9,  9,  9,
  9,  9,  9,  9,  9,  9,  9,  9,  9,  9,  9,  9,  9,  9,  9,  9,  9,  9,  9,  9, 10, 10, 10, 10,
 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 11, 11,
 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11,
 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11,
 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12,
 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12,
 12, 12, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13,
 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13,
 13, 13, 13, 13]
X_lda = pd.read_pickle('lda_values')
X_lda = np.asarray(X_lda)


plt.xlabel('1-eigenvector')
plt.ylabel('2-eigenvector')
plt.scatter(
    X_lda[:,0],
    X_lda[:,1],
    c=y,
    cmap='rainbow',
    alpha=0.7,
    edgecolors='w'
)

这是我可以从包含大量数据的大型代码中获得的最小可重现示例。

我正在做的是我为 y 中的不同类设置不同的颜色,X_lda 中的数据,其中前 50 行是蓝色,从 50 到 100 是红色,101-150 蓝色,151-200 红色。等等。

这是实际的情节:

这或多或少是我想要达到的目标。

【问题讨论】:

    标签: python python-3.x pandas matplotlib


    【解决方案1】:

    您可以使用下面的示例来绘制数据。假设有五个簇分别为 0,1,2,3,4,并且您想对 簇 0,2,4红色中的样本使用 蓝色 集群 1,3 中的样本的颜色

    import numpy as np
    from matplotlib import pyplot as plt
    
    X_lda=np.array([[1,2],[1,1],[3,3],[4,4],[2,4],[3,5],[3,4],[3,2]]) # suppose you want to plot X
    
    y=[0,1,1,1,2,3,4,4] # the cluster of each sample in X_lda 
    
    color=['b','r'] # You want to use blue for samples in clusters 0,2,4 and red color for samples in clusters 1,3 
    X_lda_colors=  [ color[i] for i in list(np.array(y)%2) ] # get the color for each sample in X_lda
    
    plt.xlabel('1-eigenvector')
    plt.ylabel('2-eigenvector')
    plt.scatter(
        X_lda[:,0],
        X_lda[:,1],
        c=X_lda_colors,
        cmap='rainbow',
        alpha=0.7,
        edgecolors='w'
    )
    plt.show()
    

    【讨论】:

      猜你喜欢
      • 2016-12-17
      • 2021-11-03
      • 1970-01-01
      • 2014-09-23
      • 1970-01-01
      • 2020-08-29
      • 1970-01-01
      • 2017-11-29
      • 1970-01-01
      相关资源
      最近更新 更多