0

I have a data set for clusters . And the output seems something like this :-

1 [1,2]

2 [1,6]

1 [2,4]

Where 1,2 ... is the cluster id and [1,2]..so on are the points . So i want to plot the points x co-ordinate and y co-ordinate on both the axis and corresponding to that a point in graph depicting the cluster id as label and for different id the color of points should be different. How do i go about it?

Thanks

1 Answer 1

1

If one axis is the cluster id I don't get how you fit both the x and y coordinates onto the other axis. So I plotted the x,y on the x and y axis and used the cluster id as a label; you can swap which value goes into which axis, I guess:

import matplotlib.pyplot as plt
from ast import literal_eval

data = """1 [1,2]

2 [1,6]

1 [2,4]"""

def cluster_color(clusternum):
    '''Some kind of map of cluster ID to a color'''
    if clusternum == 1:
        return [(1, 0,0)]
    if clusternum == 2:
        return [(0, 0, 1)]
    else:
        return [(0, 1,0)]


fig = plt.figure()
ax = fig.add_subplot(1,1,1)

def datalines(pt):
    '''Pick the coordinates and label out of this text format'''
    label, xy = pt.split(' ')
    xy = literal_eval(xy)
    assert(len(xy) == 2)
    return xy[0], xy[1], label

for pt in  data.splitlines():
    if len(pt) > 0:
        x, y, cluster = datalines(pt)
        ax.scatter(x, y, c= cluster_color(float(cluster)))
        ax.text(x + .01, y + .01, cluster)

fig.show()     

N.b.: if you have a lot of data, don't call scatter for each point separately; append x, y , cluster to three separate lists and scatter the lists.

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

2 Comments

So do you have a question, or do you mean to delete this one?
OK. I put in a crazy simple map from the cluster ID to a color; you need to know something about the format and range of possible cluster IDs to get a good general map. Check out colormap in matplotlib.

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.