469,592 Members | 1,879 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,592 developers. It's quick & easy.

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.
Expand|Select|Wrap|Line Numbers
  1. from visual import *
  2. from visual.graph import *
  3. import random
  4. import math
  5.  
  6. scene.height = scene.width = 800
  7. bob = gdisplay(width = 470, height = 400, title = 'probability of occurence v. distance from center', xtitle= ' distance', ytitle = 'probability', x = 800)
  8.  
  9.  
  10. #This forms the box
  11. roof =  box(pos=(0,5.25,0), length = 10, width = 10, height = 0.5)
  12. floor =  box(pos=(0,-5.25,0), length = 10, width = 10, height = 0.5)
  13. rwall =  box(pos=(5.25,0,0), length = 0.5, width = 10, height = 11)
  14. lwall =  box(pos=(-5.25,0,0), length = 0.5, width = 10, height = 11)
  15. bwall =  box(pos=(0,0,-5.25), length = 11, width = 0.5, height = 11)
  16.  
  17.  
  18. #This forms the ball & sphere
  19.  
  20.  
  21. joe = sphere(pos=(-4,1,2), radius = 1, color = color.red)
  22. joe.mass = 1
  23. joe.p = vector(0,0,0)
  24.  
  25. right = helix(pos = (5,0,0), length = 4, axis = (-4, 0, 0), thickness = 0.15, radius = 0.4, coils=7, color=color.blue)
  26. left = helix(pos = (-5,0,0), length = 4, axis = (4, 0, 0), thickness = 0.15, radius = 0.4, coils=7, color=color.blue)
  27. top = helix(pos = (0,5,0), length = 4, axis = (0, -4, 0), thickness = 0.15, radius = 0.4, coils=7, color=color.blue)
  28. bottom = helix(pos = (0,-5,0), length = 4, axis = (0, 4, 0), thickness = 0.15, radius = 0.4, coils=7, color=color.blue)
  29. front = helix(pos = (0,0,5), length = 4, axis = (0, 0, -4), thickness = 0.15, radius = 0.4, coils=7, color=color.blue)
  30. back = helix(pos = (0,0,-5), length = 4, axis = (0, 0, 4), thickness = 0.15, radius = 0.4, coils=7, color=color.blue)
  31.  
  32. n = 0
  33. dt = 0.01
  34. k = 0.01
  35. lst = []
  36. lf=[]
  37. ld = []
  38. lp = []
  39. while 1:
  40.     x = random.randint(0,4)
  41.     y = random.randint(0,4)
  42.     z = random.randint(0,4)
  43.     rp = (x,y,z)
  44.  
  45.     if not joe.pos==(0,0,0):
  46.         right.length = (4-((joe.pos.x**2+joe.pos.y**2)**(-2)+joe.pos.z**2)**(-2))
  47.         right.axis = (joe.pos-right.pos)
  48.  
  49.     if not joe.pos==(0,0,0):
  50.         left.length = (4-((joe.pos.x**2+joe.pos.y**2)**(-2)+joe.pos.z**2)**(-2))
  51.         left.axis = (joe.pos-left.pos)
  52.  
  53.     if not joe.pos==(0,0,0):
  54.         top.length = (4-((joe.pos.x**2+joe.pos.y**2)**(-2)+joe.pos.z**2)**(-2))
  55.         top.axis = (joe.pos-top.pos)
  56.  
  57.     if not joe.pos==(0,0,0):
  58.         bottom.length = (4-((joe.pos.x**2+joe.pos.y**2)**(-2)+joe.pos.z**2)**(-2))
  59.         bottom.axis = (joe.pos-bottom.pos)
  60.  
  61.     if not joe.pos==(0,0,0):
  62.         front.length = (4-((joe.pos.x**2+joe.pos.y**2)**(-2)+joe.pos.z**2)**(-2))
  63.         front.axis = (joe.pos-front.pos)
  64.  
  65.     if not joe.pos==(0,0,0):
  66.         back.length = (4-((joe.pos.x**2+joe.pos.y**2)**(-2)+joe.pos.z**2)**(-2))
  67.         back.axis = (joe.pos-back.pos)
  68.  
  69.     rate(1000)
  70.     if right.length > 4:
  71.         fright = vector(k*.5*(right.length-4)**2*-(right.axis))
  72.     if right.length< 4:
  73.         fright = vector(k*.5*(4-right.length)**2*(right.axis))
  74.  
  75.     if left.length > 4:
  76.         fleft = vector(k*.5*(left.length-4)**2*-(left.axis))
  77.     if left.length< 4:
  78.         fleft = vector(k*.5*(4-left.length)**2*(left.axis))
  79.  
  80.     if top.length > 4:
  81.         ftop = vector(k*.5*(top.length-4)**2*-(top.axis))
  82.     if top.length< 4:
  83.         ftop = vector(k*.5*(4-top.length)**2*(top.axis))
  84.  
  85.     if bottom.length > 4:
  86.         fbottom = vector(k*.5*(bottom.length-4)**2*-(bottom.axis))
  87.     if bottom.length< 4:
  88.         fbottom = vector(k*.5*(4-bottom.length)**2*(bottom.axis))
  89.  
  90.     if back.length > 4:
  91.         fback = vector(k*.5*(back.length-4)**2*-(back.axis))
  92.     if back.length< 4:
  93.         fback = vector(k*.5*(4-back.length)**2*(back.axis))
  94.  
  95.     if front.length > 4:
  96.         ffront = vector(k*.5*(front.length-4)**2*-(front.axis))
  97.     if front.length< 4:
  98.         ffront = vector(k*.5*(4-front.length)**2*(front.axis))
  99.  
  100.     fjoe =ffront + fback+ fright+fleft+ftop+fbottom
  101.     joe.p = vector(joe.p) + vector(fjoe*dt)
  102.     joe.pos = joe.pos + (joe.p/joe.mass)*dt
  103.  
  104.     jpx = round(joe.pos.x)
  105.     jpy = round(joe.pos.y)
  106.     jpz = round(joe.pos.z)
  107.  
  108.     if jpx == 0 and jpy == 0 and jpz == 0:
  109.         d = 0
  110.     else:
  111.         d= (jpz**2+jpy**2+jpx**2)**(0.5)
  112.  
  113.     ap = (jpx, jpy, jpz)
  114.  
  115.     if ap == rp:
  116.         lst.append(d)
  117.         l = float(len(lst))
  118.         a = float(lst.count(d))
  119.         p =  a/l
  120.         f = (d,p)
  121.         lf.append(f)
  122.         ld.append(d)
  123.         lp.append(p)
I need to graph list lf or ld versus lp
Mar 25 '10 #1
1 5797
Glenton
391 Expert 256MB
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!

Expand|Select|Wrap|Line Numbers
  1. import numpy
  2. import pylab as pl
  3.  
  4. x=4*numpy.random.random(100)
  5. y=4*numpy.random.random(100)
  6.  
  7. l=pl.plot(x,y,"ro")
  8. pl.show()
  9.  
Mar 26 '10 #2

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

30 posts views Thread by nephish | last post: by
23 posts views Thread by Brian Blais | last post: by
8 posts views Thread by Derek Basch | last post: by
2 posts views Thread by DFS | last post: by
20 posts views Thread by shapper | last post: by
reply views Thread by suresh191 | last post: by
4 posts views Thread by guiromero | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.