0

I am working on a clustering algorithm which takes input from an excel file imported with pandas as a list. The list is divided into data blocks of like 8 floating points represented by k[0], k[1].....k[7] (the index numbers correspond to values in the dictionary). The cluster is represented in dictionary form. An example of my cluster output is

cluster = {0: [0, 2, 4, 5, 6], 1: [1], 2: [3, 7]}

Is there a way to have a scatter plot so that [0, 2, 4, 5, 6] is plotted with one color, [1] in another color, [3,7] in another color. Essentially, each cluster should be marked with the same color. I would like to know how to map this list to colors (preferably as many colors as the number of clusters in the clustering algorithm which is known beforehand). I am working with matplotlib in python and am completely lost as to how to solve this problem.

Mr_U4913
  • 1,294
  • 8
  • 12
Eyejay
  • 25
  • 6
  • Have you looked at colormap implementation? [Stack Link](https://stackoverflow.com/questions/12236566/setting-different-color-for-each-series-in-scatter-plot-on-matplotlib) – anugrah Jul 13 '17 at 17:43

1 Answers1

1

You could just do a scatter for each list of numbers as follows:

import matplotlib.pyplot as plt

cluster = {0: [0, 2, 4, 5, 6], 1: [1], 2: [3, 7]}
colours = ['green', 'orange', 'red']

fig = plt.figure()
ax = fig.add_subplot(111)

for colour, (x, ys) in zip(colours, cluster.items()):
    ax.scatter([x] * len(ys), ys, c=colour, linewidth=0, s=50)

plt.show()

Giving you:

multi coloured scatter plot

To extend this to use the colour map, colours could be constructed as:

colours = cm.rainbow(np.linspace(0, 1, len(cluster)))

e.g.

import matplotlib.pyplot as plt
import matplotlib.cm as cm
import numpy as np

cluster = {0: [0, 2, 4, 5, 6], 1: [1], 2: [3, 7]}
colours = cm.rainbow(np.linspace(0, 1, len(cluster)))

fig = plt.figure()
ax = fig.add_subplot(111)

for colour, (x, ys) in zip(colours, cluster.items()):
    ax.scatter([x] * len(ys), ys, c=colour, linewidth=0, s=50)

plt.show()
Martin Evans
  • 45,791
  • 17
  • 81
  • 97
  • Thank you @ Martin Evans. This would definitely serve the purpose. I just realized, I really do not need to plot all the points that make up the index number which was what I had been trying to achieve but it isn't necessary. – Eyejay Jul 13 '17 at 20:24