Make a scatter plot of a list of ordered pairs

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jpsheehan3366
    New Member
    • Mar 2010
    • 1

    Make a scatter plot of a list of ordered pairs

    I have a list of ordered pairs and I need to make a scatter plot in python. The scatter plot needs to appear in a second window. If necessary I could graph two lists versus each-other instead of a list of ordered pairs. Here is the program that I have so far.
    Code:
    from visual import *
    from visual.graph import *
    import random
    import math
    
    scene.height = scene.width = 800
    bob = gdisplay(width = 470, height = 400, title = 'probability of occurence v. distance from center', xtitle= ' distance', ytitle = 'probability', x = 800)
    
    
    #This forms the box
    roof =  box(pos=(0,5.25,0), length = 10, width = 10, height = 0.5)
    floor =  box(pos=(0,-5.25,0), length = 10, width = 10, height = 0.5)
    rwall =  box(pos=(5.25,0,0), length = 0.5, width = 10, height = 11)
    lwall =  box(pos=(-5.25,0,0), length = 0.5, width = 10, height = 11)
    bwall =  box(pos=(0,0,-5.25), length = 11, width = 0.5, height = 11)
    
    
    #This forms the ball & sphere
    
    
    joe = sphere(pos=(-4,1,2), radius = 1, color = color.red)
    joe.mass = 1
    joe.p = vector(0,0,0)
    
    right = helix(pos = (5,0,0), length = 4, axis = (-4, 0, 0), thickness = 0.15, radius = 0.4, coils=7, color=color.blue)
    left = helix(pos = (-5,0,0), length = 4, axis = (4, 0, 0), thickness = 0.15, radius = 0.4, coils=7, color=color.blue)
    top = helix(pos = (0,5,0), length = 4, axis = (0, -4, 0), thickness = 0.15, radius = 0.4, coils=7, color=color.blue)
    bottom = helix(pos = (0,-5,0), length = 4, axis = (0, 4, 0), thickness = 0.15, radius = 0.4, coils=7, color=color.blue)
    front = helix(pos = (0,0,5), length = 4, axis = (0, 0, -4), thickness = 0.15, radius = 0.4, coils=7, color=color.blue)
    back = helix(pos = (0,0,-5), length = 4, axis = (0, 0, 4), thickness = 0.15, radius = 0.4, coils=7, color=color.blue)
    
    n = 0
    dt = 0.01
    k = 0.01
    lst = []
    lf=[]
    ld = []
    lp = []
    while 1:
        x = random.randint(0,4)
        y = random.randint(0,4)
        z = random.randint(0,4)
        rp = (x,y,z)
        
        if not joe.pos==(0,0,0):
            right.length = (4-((joe.pos.x**2+joe.pos.y**2)**(-2)+joe.pos.z**2)**(-2))
            right.axis = (joe.pos-right.pos)
    
        if not joe.pos==(0,0,0):
            left.length = (4-((joe.pos.x**2+joe.pos.y**2)**(-2)+joe.pos.z**2)**(-2))
            left.axis = (joe.pos-left.pos)
    
        if not joe.pos==(0,0,0):
            top.length = (4-((joe.pos.x**2+joe.pos.y**2)**(-2)+joe.pos.z**2)**(-2))
            top.axis = (joe.pos-top.pos)
    
        if not joe.pos==(0,0,0):
            bottom.length = (4-((joe.pos.x**2+joe.pos.y**2)**(-2)+joe.pos.z**2)**(-2))
            bottom.axis = (joe.pos-bottom.pos)
        
        if not joe.pos==(0,0,0):
            front.length = (4-((joe.pos.x**2+joe.pos.y**2)**(-2)+joe.pos.z**2)**(-2))
            front.axis = (joe.pos-front.pos)
    
        if not joe.pos==(0,0,0):
            back.length = (4-((joe.pos.x**2+joe.pos.y**2)**(-2)+joe.pos.z**2)**(-2))
            back.axis = (joe.pos-back.pos)
            
        rate(1000)
        if right.length > 4:
            fright = vector(k*.5*(right.length-4)**2*-(right.axis))
        if right.length< 4:
            fright = vector(k*.5*(4-right.length)**2*(right.axis))
    
        if left.length > 4:
            fleft = vector(k*.5*(left.length-4)**2*-(left.axis))
        if left.length< 4:
            fleft = vector(k*.5*(4-left.length)**2*(left.axis))
    
        if top.length > 4:
            ftop = vector(k*.5*(top.length-4)**2*-(top.axis))
        if top.length< 4:
            ftop = vector(k*.5*(4-top.length)**2*(top.axis))
    
        if bottom.length > 4:
            fbottom = vector(k*.5*(bottom.length-4)**2*-(bottom.axis))
        if bottom.length< 4:
            fbottom = vector(k*.5*(4-bottom.length)**2*(bottom.axis))
    
        if back.length > 4:
            fback = vector(k*.5*(back.length-4)**2*-(back.axis))
        if back.length< 4:
            fback = vector(k*.5*(4-back.length)**2*(back.axis))
    
        if front.length > 4:
            ffront = vector(k*.5*(front.length-4)**2*-(front.axis))
        if front.length< 4:
            ffront = vector(k*.5*(4-front.length)**2*(front.axis))
    
        fjoe =ffront + fback+ fright+fleft+ftop+fbottom
        joe.p = vector(joe.p) + vector(fjoe*dt)
        joe.pos = joe.pos + (joe.p/joe.mass)*dt
    
        jpx = round(joe.pos.x)
        jpy = round(joe.pos.y)
        jpz = round(joe.pos.z)
    
        if jpx == 0 and jpy == 0 and jpz == 0:
            d = 0
        else:
            d= (jpz**2+jpy**2+jpx**2)**(0.5)
    
        ap = (jpx, jpy, jpz)
    
        if ap == rp:
            lst.append(d)
            l = float(len(lst))
            a = float(lst.count(d))
            p =  a/l
            f = (d,p)
            lf.append(f)
            ld.append(d)
            lp.append(p)
    I need to graph list lf or ld versus lp
    Last edited by bvdet; Mar 25 '10, 02:05 PM. Reason: Add code tags
  • Glenton
    Recognized Expert Contributor
    • Nov 2008
    • 391

    #2
    Unless you've got a particular reason to do all the coding yourself from scratch, I would highly recommend the matplotlib/pylab modules (basically the same thing). Or gnuplot.py if you prefer that. But I think matplotlib is rapidly becoming the standard.

    There's a nice gallery of plots on the website, with the source code to achieve them, so not so hard to use (although expect a learning curve!)

    But just to make the point about coding time, here's the code for plotting a scattergram of 100 (x,y) points, with 0<x,y<4. 2 lines to generate the data. 2 lines to plot it!

    Code:
    import numpy
    import pylab as pl
    
    x=4*numpy.random.random(100)
    y=4*numpy.random.random(100)
    
    l=pl.plot(x,y,"ro")
    pl.show()

    Comment

    Working...