0

I've clustered some data using kmeans and have plotted it using scatterplot

clusters = 3
model = KMeans(n_clusters=clusters, init='k-means++', n_init=10)

model.fit(features)
labels = model.labels_

plt.scatter(features[:, 0], features[:, 1], c=model.labels_, alpha=0.75)
plt.scatter(model.cluster_centers_[:, 0], model.cluster_centers_[:, 1], marker='x')
plt.show()

The above lines can plot my clustered data as seen from below

enter image description here

But it is not showing me which color represents which cluster? How can I plot the legend accompanying this plot? For example, yellow: cluster 1, purple: cluster 2, teal: cluster 3?

StuckInPhDNoMore
  • 2,507
  • 4
  • 41
  • 73
  • 2
    What have you tried so far and what were your results? For example, matplotlib has a good [legend guide](https://matplotlib.org/stable/tutorials/intermediate/legend_guide.html) that shows the basics of adding and editing a legend for a plot – G. Anderson Apr 20 '21 at 17:40
  • 2
    Does this answer your question? [matplotlib scatter plot with color label and legend specified by c option](https://stackoverflow.com/questions/47006268/matplotlib-scatter-plot-with-color-label-and-legend-specified-by-c-option) – Ewran Apr 20 '21 at 17:51
  • 1
    @Ewran, Thank you. The linked answer did help. I did try this before but did not use a loop as suggested, thats why I was not getting the expected result – StuckInPhDNoMore Apr 20 '21 at 18:41

0 Answers0