4
votes

I am trying to plot k-means cluster using plotly, but i am having trouble in assigning colors based on there groups? I have following data frame.

enter image description here

group: cluster number

I am using this for scatter plot in plotly.

clustered.iplot(kind='scatter',x='value1',y='value2', colors = {'[clustered['group']==1]':'green', '[clustered['group']==0]':'yellow'},mode='markers',size=10)

Its wrong because it will only get True and false for color dict object. How i can can associate these group values so that color of points appear differently in the plot.

2

2 Answers

1
votes

If you only have two clusters, you can map the values:

clustered.iplot(kind='scatter', x='value1', y='value2', colors=clustered['group'].map({0:'yellow', 1:'green'}), mode='markers',size=10)
1
votes

Instead of using cufflinks you can use the new Plotly Express library (https://plotly.express) to do this with:

px.scatter(clustered, x='value1', y='value2', color='group')