I have clustered 3 features Feature1, Feature2 and Feature3 and came up with 2 clusters. I am trying to visualize a 3D cluster using matplotlib.
In the below table, there are three features upon which the clustering is executed. Number of clusters is 2.
Feature1 Feature2 Feature3 ClusterIndex
0 1.349656e-09 1.000000 1.090542e-09 0
1 1.029752e-07 1.000000 6.040669e-08 0
2 2.311729e-07 1.000000 1.568289e-11 0
3 1.455860e-08 6.05e-08 1.000000 1
4 3.095807e-07 2.07e-07 1.000000 1
Tried this code:
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
x = np.array(df['Feature1'])
y = np.array(df['Feature2'])
z = np.array(df['Feature3'])
ax.scatter(x,y,z, marker=colormap[kmeans.labels_], s=40)
However, I get the error "ValueError: could not convert string to float: red". The marker part is thus where I get the error.
2D visualization of clusters is pretty simple by plotting the points in a scatter plot and distinguishing it with cluster labels.
Just wondering is there a way to do 3D visualization of clusters.
Any suggestions would be highly appreciated !!
In principle, the code from the question should work. However it is unclear what marker=colormap[kmeans.labels_] would do and why it is needed.
The 3D scatter plot works exactly as the 2D version of it.
The marker argument would expect a marker string, like "s" or "o" to determine the marker shape.
The color can be set using the c argument. You can provide a single color or an array/a list of colors. In the example below we simply provide the cluster index to c and use a colormap.
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import pandas as pd
import numpy as np
v = np.random.rand(10,4)
v[:,3] = np.random.randint(0,2,size=10)
df = pd.DataFrame(v, columns=['Feature1', 'Feature2','Feature3',"Cluster"])
print (df)
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
x = np.array(df['Feature1'])
y = np.array(df['Feature2'])
z = np.array(df['Feature3'])
ax.scatter(x,y,z, marker="s", c=df["Cluster"], s=40, cmap="RdBu")
plt.show()

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With