Lara Hronn
Lara Hronn

Reputation: 71

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!

Upvotes: 4

Views: 6123

Answers (2)

Joel Alcedo
Joel Alcedo

Reputation: 312

Just to expand on Maxmimilian's method - if you're using sklearn version >=0.17 then you'll need to reshape your array since passing 1d arrays is deprecated in 0.17.

Here's an example with reshaping:

x = df[df.columns[1]]
x = x.values.reshape(-1,1)
y = df[df.columns[2]]
y = y.values.reshape(-1,1)

kmeans = cluster.KMeans(n_clusters = 3, random_state = 0).fit(x, y)


trace1 = go.Scatter(
x = df[df.columns[1]],
y = df[df.columns[2]],
mode = 'markers',
marker=dict(color=kmeans.labels_,
            size = 7.5,
            line = dict(width=2)
           ),
text =  df.index,
name='Actual'
)

Upvotes: -1

Maximilian Peters
Maximilian Peters

Reputation: 31659

  • 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

Upvotes: 9

Related Questions