Reputation: 25
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.
Upvotes: 0
Views: 1849
Reputation: 46759
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:
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()
Upvotes: 1