Creating a 3D Scatter Plot from your clustered data with Plotly.

Rodrigo Dutcosky
Analytics Vidhya
Published in
2 min readJul 5, 2020

--

Hello there!

Today I’ll make a quick complement from K-means Algorithm Practical Implementation with Python. If you didn’t have the chance of reading it yet, you can find it below:

If you record it, we and up clustering our data and at some point we had our DataFrame modeled like this:

Four clusters were found!

On the last post, I didn't talked much about plotting. Although, this might be the coolest part on cluster creation.

On this post I just wanted to bring out a quick tip on that. I'll use plotly.graph_objects library to create this 3d plot.

Warning: we only fed three features to train our cluster model. This gives us enough data to put these guys on a 3d scale. If you use more than three features on your cluster training, you'll need to apply some dimension reduce technique. I might discuss more about these type of technique on future posts.

Pretty much all you need from Plotly to print out a 3d scatter plot is this:

This won't separate your clusters individually tho. But we have the clustered labels on the DataFrame as well!

We could set up a loop to individually insert each cluster data into the plot.

For loop iteration, we basically will filter our DataFrame keeping all rows from each specific cluster.

Here's my changed code:

And finally, after the loop is finished we could add up some layout as we wish.

You can check all possibilities of layout updates from Plotly on its own documentation. Here:

Full Python Code to create 3D Scatterplot

Final Result

Hope you liked this quick tip and see you next time!

--

--