user2714823
user2714823

Reputation: 615

Plotting Graph in Gnuplot or using matplotlib

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

Upvotes: 0

Views: 1357

Answers (1)

cphlewis
cphlewis

Reputation: 16279

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.

Upvotes: 1

Related Questions