Re: easiest way to plot x,y graphically during run-time?

2009-06-04 Thread Nick Craig-Wood
Esmail ebo...@hotmail.com wrote:
  Scott David Daniels wrote:
  Esmail wrote:
  ... Tk seems a bit more complex .. but I really don't know much about
  it and its interface with Python to make any sort of judgments as
  to which option would be better.
  
  This should look pretty easy:
 
  Thanks Scott for taking the time to share this code with me, it
  will give me something to study. I'm not sure if I'd say it looks
  easy (but then again I am not very familiar with Tk :-)

Here is a demo with pygame...

import pygame
from pygame.locals import *
from random import randrange

size = width, height = 640, 480
background_colour = 0x00, 0x00, 0x00
foreground_colour = 0x51, 0xd0, 0x3c

def main():
pygame.init()
screen = pygame.display.set_mode(size, 0, 32)
pygame.display.set_caption(A test of Pygame - press Up and Down)
pygame.mouse.set_visible(0)
clock = pygame.time.Clock()
dots = [ (randrange(width), randrange(height)) for _ in range(100) ]
radius = 10
while True:
clock.tick(60)
for event in pygame.event.get():
if event.type == QUIT:
raise SystemExit(0)
elif event.type == KEYDOWN:
if event.key == K_ESCAPE:
raise SystemExit(0)
elif event.key == K_UP:
radius += 1
elif event.key == K_DOWN:
radius -= 1
screen.fill(background_colour)
for dot in dots:
pygame.draw.circle(screen, foreground_colour, dot, radius, 1)
dots = [ (dot[0]+randrange(-1,2), dot[1]+randrange(-1,2)) for dot in 
dots ]
pygame.display.flip()

if __name__ == __main__:
main()

-- 
Nick Craig-Wood n...@craig-wood.com -- http://www.craig-wood.com/nick
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: easiest way to plot x,y graphically during run-time?

2009-06-04 Thread Scott David Daniels

Esmail wrote:

Scott David Daniels wrote:

Esmail wrote:

... Tk seems a bit more complex .. but I really don't know much about
it and its interface with Python to make any sort of judgments as
to which option would be better.


This should look pretty easy:


Thanks Scott for taking the time to share this code with me, it
will give me something to study. I'm not sure if I'd say it looks
easy (but then again I am not very familiar with Tk :-)


I threw in too much, I think.  The reason for the idle -n trick is to
share the Tkinter mainloop with idle itself, so you can experiment with
using Tkinter and see the effects of a single command, just after you
type it in (and thus get a nice intuitive feel for the possibilities).

The tricky part to understand is the switch from imperative programming
to interrupt driven programming.  gui stuff has to switch to interrupt
driven so that it can respond to things like mouse drags, windows
repaints, and so on when they happen, rather than when the software
asks for them.  Typically, a GUI program sets some stuff up (in
imperative mode), and then switches to event-driven code and drops into
an event loop.  It is also typical of most GUI programs that the display
manipulating code (the actual painting) must all be done in a single
thread (the display thread) where the events happen.  Small calculations
can occur inside these events, but slow calculations leave your GUI
unresponsive and unable to do things like restore hidden parts as you
drag another window across it.  So, big or slow calculations are either
done in a separate threads (or single calculation thread) that
communicate back with the display thread, or the big slow calculation
are done in bite-sized pieces in the display thread, doing a piece and
moving the rest on to another event.

A Tkinter canvas is nice to use because you can draw things on it, move
those things around (separately or in groups), change their shape or
color, make them visible or invisible, and the canvas keeps track of
drawing them.

import Tkinter as tk

# First set up Tkinter and a root window (idle -n fakes part)
root = tk.Tk()

# Set that window's size (400x400) and loc (5,5)
root.geometry('400x400+5+5')

# make a canvas (2-D drawing area) in that window
canvas = tk.Canvas(root)

#make the canvas fill the window (even as the window is resized)
canvas.pack(expand=1, fill=tk.BOTH)

# draw a red filled oval on the canvas bounded by (50,100), (70,140)
a = canvas.create_oval((50, 100, 70, 140), fill='red')

# the hard-to-get part about the example is that the Mover class
# takes a canvas element or tag (such as a above), and provides a
# couple of methods (.start(event) and .move(event)) for associating
# with the mouse.  .start when the button goes down, and .move
# as the button is dragged with the mouse down.

canvas.itemconfig(a, fill='#55AADD') # Change to oval to near cyan
canvas.move(a, 5, 5) # move it down right 5 pixels

single_mover = Mover(canvas, a) # associate motion with our oval
# make the left button down on canvas call our start method
# which just gives u a point to move around from.
canvas.bind(Button-1, single_mover.start)

# make motion while left button is down call our move method.
# there we will compare the mouse position to our saved location,
# move our oval (in this case) a corresponding distance, and
# update the saved location.
canvas.bind(B1-Motion, single_mover.move)

# at this point we have our behavior wired up. If in idle, just
# try it, but if not:
tk.mainloop() # stays there until you click the close window.

Also, a Tkinter Canvas can be saved as an encapsulated postscript file,
thus allowing you pretty pictures into PDFs.

--Scott David Daniels
scott.dani...@acm.org
--
http://mail.python.org/mailman/listinfo/python-list


Re: easiest way to plot x,y graphically during run-time?

2009-06-04 Thread Peter Pearson
On Thu, 04 Jun 2009 03:29:42 -0500, Nick Craig-Wood wrote:
[snip]
 Here is a demo with pygame...
[snip]

And just for completeness, here is a demo with PyGUI, written
in similar style.  (I'm a PyGUI newbie, so constructive criticism
would be appreciated.)

from GUI import Window, View, application, Color, Task
from random import randrange

class Brownian( View ):

  def __init__( self, **kwargs ):
View.__init__( self, **kwargs )
width, height = self.size
self.dots = [ (randrange(width), randrange(height))
  for _ in range( 100 ) ]

  def step( self ):
self.dots = [ (dot[0]+randrange(-1,2), dot[1]+randrange(-1,2))
  for dot in self.dots ]
self.invalidate()

  def draw( self, canvas, rectangle ):
canvas.set_backcolor( Color( 0, 0, 0 ) )
canvas.set_forecolor( Color( 0.3, 0.85, 0.25 ) )
radius = 10
canvas.erase_rect( rectangle )
for dot in self.dots:
  canvas.stroke_oval( ( dot[0]-radius, dot[1]-radius,
dot[0]+radius, dot[1]+radius ) )


def main():

  size = 640, 480
  win = Window( size = size, title = A test of PyGUI )
  b = Brownian( size = size )
  win.add( b )
  win.show()
  t = Task( b.step, interval = 0.02, repeat = True, start = True )
  application().run()
  
if __name__ == __main__:
main()


-- 
To email me, substitute nowhere-spamcop, invalid-net.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: easiest way to plot x,y graphically during run-time?

2009-06-04 Thread Esmail

Nick Craig-Wood wrote:


Here is a demo with pygame...



Thanks Nick, I'll be studying this too :-)

Esmail

--
http://mail.python.org/mailman/listinfo/python-list


Re: easiest way to plot x,y graphically during run-time?

2009-06-03 Thread Diez B. Roggisch
Esmail wrote:

 Hi all,
 
 I am trying to visualize a number of small objects moving over
 a 2D surface during run-time. I was wondering what would the easiest
 way to accomplish this using Python? Ideally I am looking for a shallow
 learning curve and efficient implementation :-)
 
 These objects may be graphically represented as dots, or preferably
 as small arrow heads/pointy triangles moving about as their x,y
 coordinates change during run-time.

pygame certainly suited, but also Tk with it's canvas-object.

Diez
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: easiest way to plot x,y graphically during run-time?

2009-06-03 Thread Gökhan SEVER
It seems like you want to animate your data.

You may want to take a look at Matplotlib examples or Mayavi for 3D
animations
(
http://code.enthought.com/projects/mayavi/docs/development/html/mayavi/mlab_animating.html?highlight=animation
)

Gökhan


On Wed, Jun 3, 2009 at 10:53 AM, Esmail ebo...@hotmail.com wrote:

 Hi all,

 I am trying to visualize a number of small objects moving over
 a 2D surface during run-time. I was wondering what would the easiest
 way to accomplish this using Python? Ideally I am looking for a shallow
 learning curve and efficient implementation :-)

 These objects may be graphically represented as dots, or preferably
 as small arrow heads/pointy triangles moving about as their x,y
 coordinates change during run-time.

 Thanks,
 Esmail

 --
 http://mail.python.org/mailman/listinfo/python-list

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: easiest way to plot x,y graphically during run-time?

2009-06-03 Thread Esmail

Gökhan SEVER wrote:

It seems like you want to animate your data.

You may want to take a look at Matplotlib examples or Mayavi for 3D 


I've used Matplotlib to plot points that were saved during runtime to
a file. I wonder if I could skip that step and directly plot during
runtime updating the graph as values changed ..

I've only heard about Mayavi, so I'll check it out.

Thanks Gökhan,

Esmail

--
http://mail.python.org/mailman/listinfo/python-list


Re: easiest way to plot x,y graphically during run-time?

2009-06-03 Thread Esmail

Gökhan SEVER wrote:
I don't know how easy to use pygame or pyOpenGL for data animation 
comparing to Mayavi.


Mayavi uses VTK as its visualization engine which is an OpenGL based 
library. I would like to learn more about how alternative tools might be 
beneficial say for example atmospheric particle simulation or realistic 
cloud simulation.


I've just started to read more about Particle Swarm Optimization and
since I plan to implement this in Python, I thought it would be nice
to visualize the process too, without spending too much on the nitty
gritty details of graphics.

Esmail, you may ask your question in Matplotlib users group for more 
detailed input.


good idea, thanks,

Esmail

--
http://mail.python.org/mailman/listinfo/python-list


Re: easiest way to plot x,y graphically during run-time?

2009-06-03 Thread ma
Try out PyChart, it's a very complete and has a great interface. I use
it to generate statistics for some of our production machines:
http://home.gna.org/pychart/

On Wed, Jun 3, 2009 at 1:28 PM, Esmail ebo...@hotmail.com wrote:
 Gökhan SEVER wrote:

 I don't know how easy to use pygame or pyOpenGL for data animation
 comparing to Mayavi.

 Mayavi uses VTK as its visualization engine which is an OpenGL based
 library. I would like to learn more about how alternative tools might be
 beneficial say for example atmospheric particle simulation or realistic
 cloud simulation.

 I've just started to read more about Particle Swarm Optimization and
 since I plan to implement this in Python, I thought it would be nice
 to visualize the process too, without spending too much on the nitty
 gritty details of graphics.

 Esmail, you may ask your question in Matplotlib users group for more
 detailed input.

 good idea, thanks,

 Esmail

 --
 http://mail.python.org/mailman/listinfo/python-list

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: easiest way to plot x,y graphically during run-time?

2009-06-03 Thread Mensanator
On Jun 3, 10:53 am, Esmail ebo...@hotmail.com wrote:
 Hi all,

 I am trying to visualize a number of small objects moving over
 a 2D surface during run-time. I was wondering what would the easiest
 way to accomplish this using Python?

Try Turtle Graphics using goto's. With pen up! :-)

 Ideally I am looking for a shallow
 learning curve and efficient implementation :-)

 These objects may be graphically represented as dots, or preferably
 as small arrow heads/pointy triangles moving about as their x,y
 coordinates change during run-time.

 Thanks,
 Esmail

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: easiest way to plot x,y graphically during run-time?

2009-06-03 Thread Esmail

Mensanator wrote:

On Jun 3, 10:53 am, Esmail ebo...@hotmail.com wrote:

Hi all,

I am trying to visualize a number of small objects moving over
a 2D surface during run-time. I was wondering what would the easiest
way to accomplish this using Python?


Try Turtle Graphics using goto's. With pen up! :-)


hehe .. yes, I briefly considered this too ...

--
http://mail.python.org/mailman/listinfo/python-list


Re: easiest way to plot x,y graphically during run-time?

2009-06-03 Thread Esmail

ma wrote:

Try out PyChart, it's a very complete and has a great interface. I use
it to generate statistics for some of our production machines:
http://home.gna.org/pychart/


Thanks for the suggestion and link, I'm not familiar with this, but
will check it out.

If I can get matlibplot to work it would be fine since I've already
used it a bit before - so there would be less of a learning curve.

Regards,
Esmail

--
http://mail.python.org/mailman/listinfo/python-list


RE: easiest way to plot x,y graphically during run-time?

2009-06-03 Thread esmail bonakdarian

Hi Brian,

Thanks for the code sample, that looks quite promising. I can
run it and understand most of it - my knowledge of pylab/matplotlib is
still quite rudimentary. I wish there was a good manual/tutorial that
could be printed off (or for that matter a book) on this as it seems
quite cabable and feature rich.

Esmail
---

here is a sample.  again, direct questions to the matplotlib list for
possible better ideas.


from pylab import *

# initial positions
x0=rand(5)
y0=rand(5)

ion()  # interactive on

for t in linspace(0,10,100):

x=x0+0.1*cos(t)
y=y0+0.1*sin(t)

if t==0:  # first time calling
h=plot(x,y,'o')
else:
h[0].set_data(x,y)

draw()



bb

-- 
Brian Blais
bbl...@bryant.edu
http://web.bryant.edu/~bblais



_
Windows Live™ SkyDrive™: Get 25 GB of free online storage.
http://windowslive.com/online/skydrive?ocid=TXT_TAGLM_WL_SD_25GB_062009
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: easiest way to plot x,y graphically during run-time?

2009-06-03 Thread Brian Blais


On Jun 3, 2009, at 12:15 , Esmail wrote:


Gökhan SEVER wrote:

It seems like you want to animate your data.
You may want to take a look at Matplotlib examples or Mayavi for 3D


I've used Matplotlib to plot points that were saved during runtime to
a file. I wonder if I could skip that step and directly plot during
runtime updating the graph as values changed ..


here is a sample.  again, direct questions to the matplotlib list for  
possible better ideas.



from pylab import *

# initial positions
x0=rand(5)
y0=rand(5)

ion()  # interactive on

for t in linspace(0,10,100):

x=x0+0.1*cos(t)
y=y0+0.1*sin(t)

if t==0:  # first time calling
h=plot(x,y,'o')
else:
h[0].set_data(x,y)

draw()



bb

--
Brian Blais
bbl...@bryant.edu
http://web.bryant.edu/~bblais



-- 
http://mail.python.org/mailman/listinfo/python-list


Re: easiest way to plot x,y graphically during run-time?

2009-06-03 Thread Scott David Daniels

Esmail wrote:

... Tk seems a bit more complex .. but I really don't know much about
it and its interface with Python to make any sort of judgments as
to which option would be better.


This should look pretty easy:


import Tkinter as tk

class Mover(object):
def __init__(self, canvas, tag_or_id):
self.canvas = canvas
self.ident = tag_or_id
self.x = self.y = 0

def start(self, event):
self.x = event.x
self.y = event.y

def move(self, event):
if event.x != self.x or event.y != self.y:
dx = event.x - self.x
dy = event.y - self.y
self.x = event.x
self.y = event.y
self.canvas.move(self.ident, dx, dy)

def setup(root=None):
if root is None:
root = tk.Tk()
root.geometry('400x400+5+5')  # experiment: -- place and size
canvas = tk.Canvas(root)
canvas.pack(expand=1, fill=tk.BOTH)
# ovals are x1, y1, x2, y2
a = canvas.create_oval((50, 100, 70, 140), fill='red')
b = canvas.create_oval((100, 200, 140, 290), fill='blue')
c = canvas.create_oval((300, 300, 390, 330), fill='green')

canvas.itemconfig(a, fill='#55AADD') # using internet colors
canvas.move(a, 5, 5) # move a down right 5 pixels
mover = [Mover(canvas, ident) for ident in (a, b, c)]
canvas.bind(B1-Motion, mover[0].move)
canvas.bind(Button-1, mover[0].start)
canvas.bind(B2-Motion, mover[1].move)
canvas.bind(Button-2, mover[1].start)
canvas.bind(B3-Motion, mover[2].move)
canvas.bind(Button-3, mover[2].start)
return canvas, mover

if __name__ == '__main__':
c, m = setup()
tk.mainloop()


If you want to experiment, use something like:
python -m idlelib.idle -n
or
$ python wherever python is/Lib/idlelib/idle.py -n
or
C:\ C:\Python25\python C:\Python25\Lib\idlelib\idle.py -n

To get an idle window with the -n switch on (so you are using the idle
in-process) to see the effects of each Tkinter operation as you go.
You can then run these operations in place, seeing results and effects.

--Scott David Daniels
scott.dani...@acm.org
--
http://mail.python.org/mailman/listinfo/python-list


Re: easiest way to plot x,y graphically during run-time?

2009-06-03 Thread Esmail

Scott David Daniels wrote:

Esmail wrote:

... Tk seems a bit more complex .. but I really don't know much about
it and its interface with Python to make any sort of judgments as
to which option would be better.


This should look pretty easy:


Thanks Scott for taking the time to share this code with me, it
will give me something to study. I'm not sure if I'd say it looks
easy (but then again I am not very familiar with Tk :-)

Best,
Esmail

--
http://mail.python.org/mailman/listinfo/python-list