0

I'm trying to plot K-Means Clustering(3) using matplotlib on Spyder IDE. The code runs without error. However, ironically the cmap is not applied to the graph, thus showing a basic blue colored scatter plot as result.

plt.scatter(df['X'], df['Y'], s=50, cmap='viridis')

What changes should I make to get each cluster colored differently?

ariasj
  • 1
  • 2
  • From the [docs](https://stackoverflow.com/questions/17682216/scatter-plot-and-color-mapping-in-python): A Colormap instance or registered colormap name. `cmap` is only used if `c` is an array of floats. Also demonstrated [here](https://stackoverflow.com/questions/17682216/scatter-plot-and-color-mapping-in-python). – BigBen Mar 03 '21 at 14:05
  • Thank you, problem solved. The hyperlinks lead to an identical post tho. If anyone runs into a similar issue, I found the following article very helpful as well: https://betterprogramming.pub/how-to-use-colormaps-with-matplotlib-to-create-colorful-plots-in-python-969b5a892f0c. – ariasj Mar 03 '21 at 15:42

1 Answers1

0

SOLUTION FOUND: in order to use matplotlib cmap, instead of color argument, you use c and cmap. Plus, as @BigBen mentions in the comment, c has to be an array of floats. To solve my issue, I revised the code as following:

    plt.scatter(df['X'], df['Y'], s=50, c=pred_vals, cmap='viridis')

I found these very useful:

ariasj
  • 1
  • 2