'How to put label in Clustering plotted image : indicating in the side which cluster it is, and also in the center?
i used k means clustering and would like to put label both in the side indicating the colors and in the center telling which cluster centroid it is, Can anybody please help? i tried the solutions, but it didnt help. Thank you. My code is as folows:
i also tried to map the name but it didnt work well. # map clusters to appropriate labels cluster_map = {0: "0", 1: "1", 2: "2", 3:"3" , 4:"4", 5: "5"} # apply mapping df['cluster'] = df['cluster'].map(cluster_map)
My code is as follows:
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);
Solution 1:[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.
from sklearn.cluster import KMeans
from sklearn.datasets import make_blobs
from itertools import cycle
import matplotlib.pyplot as plt
import matplotlib.cm as cm
n_clusters = 4
X, y_true = make_blobs(n_samples=300, centers=n_clusters,
cluster_std=0.60, random_state=0)
kmeans = KMeans(n_clusters=n_clusters)
kmeans.fit(X)
y_kmeans = kmeans.predict(X)
centers = kmeans.cluster_centers_
# map predictions to label
labels = {0: "Cluster 1", 1: "Cluster 2", 2: "Cluster 3", 3: "Cluster 4"}
colors = cycle(cm.tab10.colors)
plt.figure()
for i in range(n_clusters):
# plot one cluster for each iteration
color = next(colors)
# find indeces corresponding to cluser i
idx = y_kmeans == i
# plot cluster
plt.scatter(X[idx, 0], X[idx, 1], color=color, s=50, label=labels[i], alpha=0.25)
# plot center
plt.scatter(centers[i, 0], centers[i, 1], edgecolors="k", linewidth=2, color=color, s=200, alpha=1)
plt.legend()
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Davide_sd |


