我正在尝试为DBscan绘制的每个群集着色不同的颜色。我创建了一个颜色列表,并尝试使代码遍历每个群集并为其绘制不同的颜色,但是它为所有群集上相同的颜色,这是颜色列表中的最后一个颜色(紫色)。帮助将不胜感激。
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()
[![在此处输入图片描述] [1]] [1]
答案 0 :(得分:1)
我能够解决它,所以我在回答,以防将来有人想要一个可能的答案。 我需要做的就是获取我的DBscan数据,并使用fit_predict和(x,y)绘图点(在我的例子中为“簇”)。
像这样:
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()
答案 1 :(得分:0)
我认为这会起作用:
color_list = np.array(['green', 'blue', 'red', 'yellow', 'orange', 'magenta', 'cyan', 'purple'])
plt.scatter(clusters['X'], clusters['Y'], c = color_list[labels])
plt.show()