Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scatter plot segregate clusters by color plotly python

I am using plotly (to be able to get point information when I hoover over) to visualise my clustered scatter plot. I am having trouble with assigning different colours to the clusters I have produced by using KMeans. When plotting this in matplotlib.pyplot (as plt) I use the following code:

plt.scatter(restult[:,0], result[:,1], c=cluster_labels

cluster_labels being:

n_clusters = 3
km = KMeans(n_clusters).fit(result)
labels = km.labels_

And it works totally fine, but I need the hoover info.

This is where I am at so far with plotly:

trace = go.Scatter(
    x = result[:,0],
    y = result[:,1],
    mode = 'markers',
    text = index, # I want to see the index of each point
)
data = [trace]

# Plot and embed in ipython notebook!
py.iplot(data, filename='basic-scatter')

I appreciate the help!

like image 567
Lara Hronn Avatar asked Oct 23 '25 02:10

Lara Hronn


1 Answers

  • Let's use the iris data set
  • The labels from kmeans are used as colors (marker=dict(color=kmeans.labels_)), just like in matplotlib

from sklearn import datasets
from sklearn import cluster
import plotly
plotly.offline.init_notebook_mode()

iris = datasets.load_iris()
kmeans = cluster.KMeans(n_clusters=3, 
                        random_state=42).fit(iris.data[:,0:2])
data = [plotly.graph_objs.Scatter(x=iris.data[:,0], 
                                  y=iris.data[:,1], 
                                  mode='markers',     
                                  marker=dict(color=kmeans.labels_)
                                  )
       ]
plotly.offline.iplot(data)

enter image description here

like image 132
Maximilian Peters Avatar answered Oct 25 '25 01:10

Maximilian Peters