如何将标签放在群集绘制的图像中:在侧面指示它是哪个群集,以及中心?
我使用K表示聚类,并想将标签放在指示颜色的侧面,并在中心介绍哪个集群质心,有人可以帮忙吗?我尝试了解决方案,但没有帮助。谢谢。 我的代码是folows:
我也尝试映射名称,但效果不佳。 #地图簇到适当的标签 cluster_map = {0:“ 0”,1:“ 1”,2:“ 2”,3:“ 3”,4:“ 4”,5:“ 5”} #应用映射 df ['cluster'] = df ['cluster']。map(cluster_map)
我的代码如下:
import seaborn as sns
import matplotlib.pyplot as plt
from sklearn.decomposition import PCA
sklearn_pca = PCA(n_components = 2)
Y_sklearn = sklearn_pca.fit_transform(X_train_vc.toarray())
kmeans = KMeans(n_clusters=k_clusters, max_iter=600, algorithm = 'auto')
fitted = kmeans.fit(Y_sklearn)
prediction = kmeans.predict(Y_sklearn)
plt.figure(figsize=(12, 6))
plt.scatter(Y_sklearn[:, 0], Y_sklearn[:, 1], c=prediction, s=40, cmap='viridis', linewidths=5)
centers = fitted.cluster_centers_
plt.scatter(centers[:, 0], centers[:, 1],c='black', s=200, alpha=0.6);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
由于您没有提供数据,因此我会想出自己的数据。
要使用图例,您需要为每个群集添加一个散点图(带有标签)。
在我看来,您应该使用散点参数来使人们直观地看到质心属于给定的群集,而不是将文本放在图表上以表明质心。因此,质心使用更大的标记,具有快速识别的边缘颜色,设置其面部颜色和完全不透明度。群集使用的不透明度值要低得多。
Since you didn't provide data, I'm going to come up with my own.
To use a legend, you need to add a scatter plot (with its label) for each cluster.
In my opinion, rather than putting texts on the figure to indicate the centroids, you should play with the scatter parameters to make it intuitive for people to see that a centroid belongs to a given cluster. Hence, the centroids use a bigger marker, with a quickly identifiable edge color, setting its face color and full opacity. The clusters uses a much lower opacity value.