在散点图中为每个类别绘制不同的聚类标记 [英] Plotting different clusters markers for every class in scatter plot

查看:112
本文介绍了在散点图中为每个类别绘制不同的聚类标记的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个散点图,其中我在绘制14个聚类,但是每个2个聚类属于同一类,它们都使用相同的标记.每50行是一个群集,每100行是两个相同类的群集.我想做的是每2个群集或100行更改标记.

这是我正在努力实现的目标.

我得到的错误:

  ValueError:无法识别的标记样式['x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x'...]' 

解决方案

请尝试以下操作:

 将numpy导入为np从matplotlib导入pyplot作为pltX_lda = np.array([[1,2],[1,1],[3,3],[4,4],[2,4],[3,5],[3,4],[3,2]])#假设您要绘制Xy = [0,1,1,1,2,3,4,4]#X_lda中每个样本的簇color = ['b','r']标记= ['x','o','1','.','2','>','D']#标记X_lda_colors = [list [np.array(y)%2)中i的color [i]]X_lda_markers = [list(np.array(y)%2)中i的markers [i]]plt.xlabel('1-特征向量')plt.ylabel('2-特征向量')对于范围内的我(X_lda.shape [0]):plt.scatter(X_lda [i,0],X_lda [i,1],c = X_lda_colors [i],marker = X_lda_markers [i],cmap ='rainbow',alpha = 0.7,edgecolors ='w')plt.show() 

I have a scatter plot where i am plotting 14 clusters, but each 2 clusters belong to the same class, they are all using the same markers. Every 50 rows is a cluster and every 100 rows is two clusters of the same class. What i want to do is change the markers for every 2 clusters or 100 rows.

Link for the Data Frame

    import pandas as pd
    import numpy as np
    from matplotlib import pyplot as plt
    from matplotlib.pyplot import figure
    
    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)
    
    

markers = ['x', 'o', '1', '.', '2', '>', 'D']
color=['b','r'] 
X_lda_colors=  [ color[i] for i in list(np.array(y)%2) ]
X_lda_markers = [markers[i] for i in list(np.array(y)%2)] 
plt.xlabel('1-eigenvector')
plt.ylabel('2-eigenvector')
plt.scatter(
    X_lda[:,0],
    X_lda[:,1],
    marker = X_lda_markers,
    c=X_lda_colors,
    cmap='rainbow',
    alpha=0.7,
)

This is the minimal reproducible example i could get from a large code with large data.

This is the actual plot:

This is what i am trying to achieve.

The error i get:

ValueError: Unrecognized marker style ['x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x'...]'

解决方案

Please try this:

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'] 
markers = ['x', 'o', '1', '.', '2', '>', 'D'] # marker
X_lda_colors=  [ color[i] for i in list(np.array(y)%2) ] 
X_lda_markers= [ markers[i] for i in list(np.array(y)%2) ] 
plt.xlabel('1-eigenvector')
plt.ylabel('2-eigenvector')

for i in range(X_lda.shape[0]):
    plt.scatter( X_lda[i,0],    X_lda[i,1],    c=X_lda_colors[i],
    marker=X_lda_markers[i],    cmap='rainbow',   alpha=0.7,     edgecolors='w')
plt.show()

这篇关于在散点图中为每个类别绘制不同的聚类标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆