简体   繁体   English

想要为每个盆栽簇涂上不同的颜色

[英]Want to Color Each Potted Cluster a Different Color

I'm trying to color each cluster plotted from DBscan a different color.我正在尝试为从 DBscan 绘制的每个集群着色不同的颜色。 I created a color list and tried to have the code iterate through each cluster and plot it a different color, but it colors all the clusters the same color which is the last color from the color list (purple).我创建了一个颜色列表,并尝试让代码遍历每个集群并绘制不同的颜色,但它为所有集群着色相同的颜色,这是颜色列表中的最后一个颜色(紫色)。 Help would be appreciated.帮助将不胜感激。

import pandas as pd
from sklearn.cluster import DBSCAN
from collections import Counter
from sklearn.neighbors import NearestNeighbors
from matplotlib import pyplot as plt
import matplotlib
matplotlib.use('TkAgg')

eps = 4.3


model  = DBSCAN(eps            = eps,
                min_samples    = 210,
                metric         = 'euclidean'
                )

data = model.fit(plotting_data)


X          = plotting_data['X']
Y          = plotting_data['Y']

clusters = data.fit_predict(plotting_data)
print(clusters)

clust_df = pd.DataFrame(plotting_data)


clusters = (clust_df[data.labels_ != -1])



labels = data.labels_
num_clusters = len(set(labels))
print(num_clusters)



color_list = ['green', 'blue', 'red', 'yellow',  'orange',  'magenta', 'cyan', 'purple']
labels = data.labels_
num_clusters = len(set(labels))


i = 0
for col in zip(color_list):
    plt.scatter(clusters['X'],
                clusters['Y'],
                c = col

                )
    i += 1


plt.title("Clusters: " + str(num_clusters), fontsize = 13)

plt.show()

[![enter image description here][1]][1] [![在此处输入图像描述][1]][1]

I was able to solve it, so I'm answering in case someone in the future wants a possible answer.我能够解决它,所以我会回答以防将来有人想要一个可能的答案。 All I needed to do was take my DBscan data and use fit_predict with the (x,y) plotting points ('clusters', in my case).我需要做的就是获取我的 DBscan 数据并将 fit_predict 与 (x,y) 绘图点(在我的情况下为“集群”)一起使用。

Like so:像这样:

color_clusters = data.fit_predict(clusters)

# i = 0
# for col in zip(color_list):

plt.scatter(clusters['X'],
           clusters['Y'],
           c = color_clusters,
           cmap = 'inferno'

           )
plt.colorbar()

I think this would work:我认为这会奏效:

color_list = np.array(['green', 'blue', 'red', 'yellow',  'orange',  'magenta', 'cyan', 'purple'])

plt.scatter(clusters['X'], clusters['Y'], c = color_list[labels])
plt.show()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM