1,105,578 Community Members

How to draw Scatter Plot using Python

Member Avatar
srinivasanb4u
Newbie Poster
5 posts since Oct 2008
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Hi
I am new to python . I need source code to draw scatter plot using Python

my Input_1 is something like below:
-------------------------------------------
ID X Y Z
120321 172.15 -285.03 -870.09
120323 244.31 -257.18 -870.19
120324 274.65 -277.03 -870.10
120327 226.51 -263.98 -870.09
120328 268.55 -271.05 -870.10


my Input_2 is something like below:
-------------------------------------------

120321 ST
120323 SL
120324 ST
120327 ST
120328 OP

My problem is it has to read File Input_1 x,y,z data and plot the points in space such that
if it is "ST" i should get point in Green Color, "SL" means RED color, "OP" means black color

I am attching sample image for this


Can any body help in the code for the above problem

Attachments untitled.JPG 68.07KB
Member Avatar
Gribouillis
Posting Maven
3,456 posts since Jul 2008
Reputation Points: 1,140 [?]
Q&As Helped to Solve: 884 [?]
Skill Endorsements: 18 [?]
Moderator
 
0
 

Here is a code which should read your file and produce a python list of points. Check that it works.

colors = {
    "ST": "green",
    "SL": "red",
    "OP": "black",
    }

point_colors = {}

class Point(object):
    "class for point objects"
    def __init__(self, id, x, y, z):
        self.id = id
        self.x = x
        self.y = y
        self.z = z
        
    @property
    def color(self):
        return colors[point_colors[self.id]]
    def __repr__(self):
        return "Point(%d, %f, %f, %f)" % (self.id, self.x, self.y, self.z)

def read_files():
    "reads the files and return a list of points"
    the_points = []
    for line in open("Input_1"):
        try:
            L = line.strip().split()
            if len(L) != 4:
                continue
            id, x, y, z = int(L[0]), float(L[1]), float(L[2]), float(L[3])
            point = Point(id, x, y, z)
            the_points.append(point)
        except:
            pass
    for line in open("Input_2"):
        try:
            L = line.strip().split()
            if len(L) != 2:
                continue
            id, col = int(L[0]), L[1]
            point_colors[id] = L[1]
        except:
            pass
    return the_points

the_points = read_files()

if __name__ == "__main__":
    for point in the_points:
        print repr(point)

From this, you must choose a graphic library to plot the points. I suppose that you want to plot the points only in 2D ? See if you can install matplotlib, which has simple commands and nice output.
http://matplotlib.sourceforge.net

Member Avatar
srinivasanb4u
Newbie Poster
5 posts since Oct 2008
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

thankyou for u r Reply

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article