I'm unable to plot the data for K mean clusering algo usingsklearn as it throws this error : TypeError: scatter() missing 1 required positional argument: 'y'

Here is the function I have written to plot my clusters:

from sklearn.cluster import KMeans
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import pandas as pd
import numpy as np
%matplotlib inline
from sklearn import datasets

kmeans = KMeans(n_clusters=3).fit(df)
centroids = kmeans.cluster_centers_


After I write plot k mean:

plt.scatter(df, c= kmeans.labels_.astype(float), s=50, alpha=0.5)
plt.scatter(centroids[:, 0], centroids[:, 1], c='red', s=50)


What is the best way to plot clusters in python?


You need to be explicit on what is the x axis in the plot, and what attribute is the y axis. You are only passing the entire data frame - pass two columns instead.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.