0

I am trying to plot legends for the attached image and I am unable to plot legend with my below query, Could someone please help me resolve this?

#Predicted Labels on PCA
pcadf = pd.DataFrame(preprocessed_data)
pcadf["kmeans"] = pipe["clusterer"]["kmeans"].labels_
pcadf.columns = ['component_1', 'component_2', 'kmeans']

x = pcadf['component_1'].values
y = pcadf['component_2'].values

Cluster = pcadf["kmeans"].values
fig = plt.figure(figsize=(10,5))

ax = fig.add_subplot(111)
scatter = ax.scatter(x,y,c=Cluster,s=50)
ax.legend()
fig.savefig('KMeans_Cluster.png', bbox_inches='tight', dpi=1200)

enter image description here

1 Answer 1

1

See this matplotlib help page for the 2 options. You either loop through the different labels, or use the PathCollection's legend_elements() , and below I use an example for the 2nd option:

from sklearn.decomposition import PCA
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.cluster import KMeans
from sklearn.preprocessing import StandardScaler

iris = sns.load_dataset('iris')
df = StandardScaler().fit_transform(iris.iloc[:,:4])

pcadf = PCA(n_components=2).fit_transform(df)
pcadf = pd.DataFrame(pcadf,columns = ['component_1','component_2'])
pcadf["kmeans"] = KMeans(n_clusters=2).fit_predict(df)

#Cluster = pcadf["kmeans"].values
fig = plt.figure(figsize=(10,5))
ax = fig.add_subplot(111)
ax.scatter(pcadf['component_1'],pcadf['component_2'],c=pcadf['kmeans'],s=50)
legend1 = ax.legend(*scatter.legend_elements(),
                    loc="lower left", title="Clusters")
ax.add_artist(legend1)

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.