title: How to plot clusters and centers from a multi-feature kmeans model, with Matplotlib?
body: <p>I used <code>kmeans</code> algorithm to determine number of clusters in my dataset. In the following code, you can see that I have multiple features, some are categorical some are not. I encoded, and scaled them, and I get my optimal number of clusters.</p>
  <p>You can download data from here:
  <a href="https://www.sendspace.com/file/1cnbji" rel="nofollow noreferrer">https://www.sendspace.com/file/1cnbji</a></p>
  <pre><code>import sklearn.metrics as sm
  
  from sklearn.preprocessing import scale
  
  from sklearn.preprocessing import Normalizer
  from sklearn.preprocessing import StandardScaler, MinMaxScaler
  
  from sklearn.cluster import KMeans, SpectralClustering, MiniBatchKMeans
  from sklearn.compose import ColumnTransformer
  from sklearn.preprocessing import OneHotEncoder
  
  import matplotlib.pyplot as plt
  
  import pandas as pd
  
  
  
  df = pd.read_csv('dataset.csv')
  print(df.columns)
  
  features = df[['parcela', 'bruto', 'neto',
                 'osnova', 'sipovi', 'nadzemno',
                 'podzemno', 'tavanica', 'fasada']]
  
  trans = ColumnTransformer(transformers=[('onehot', OneHotEncoder(), ['tavanica', 'fasada']),
                                          ('StandardScaler', Normalizer(), ['parcela', 'bruto', 'neto', 'osnova', 'nadzemno', 'podzemno', 'sipovi'])],
                            remainder='passthrough') # Default is to drop untransformed columns
  
  features = trans.fit_transform(features)
  
  Sum_of_squared_distances = []
  for i in range(1,19):
  
       kmeans = KMeans(n_clusters = i, init = 'k-means++', random_state = 0)
       kmeans.fit(features)
       Sum_of_squared_distances.append(kmeans.inertia_)
  
  
  plt.plot(range(1,19), Sum_of_squared_distances, 'bx-')
  plt.xlabel('k')
  plt.ylabel('Sum_of_squared_distances')
  plt.title('Elbow Method For Optimal k')
  plt.show()
  </code></pre>
  <p><a href="https://i.stack.imgur.com/FOKDn.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/FOKDn.png" alt="enter image description here" /></a></p>
  <ul>
  <li>On the graph, the elbow method shows my optimal number of clusters as 7.</li>
  <li>How can I plot the 7 clusters?
  <ul>
  <li>I want to see centroids on the graph, and scatter plot with 7 different colors of the clusters.</li>
  </ul>
  </li>
  </ul>