AstroPy - tplots.py


Description

This file contains classes for threaded versions of the plots in plots.py, so that wx graphics can be used from an interactive Python session without blocking the interpreter. This module runs the wx GUI from a separate thread, and makes all graphics calls to that thread. Common data can be accessed from both the main and the GUI thread. The threading is transparent to the user, and is started automatically when the module is imported.

Usage

NewPlot(ptype, *args, **kwargs)
   Create a new 1d, 2d, or image plot.  ptype is '1d', '2d', or 'im'.  args and kwargs
   are the same arguments and keyword arguments you would pass to the corresponding 
   class call in plots.py.

reload(*args, **kwargs)
   Plot new data.  args and kwargs are the same arguments and keyword arguments you would 
   pass to the corresponding class call in plots.py.

setTitle(title)
   Set the title of the plot.

GetPlot(id)
   Get the wx window from the plot's id (plot.id).

You can also access the wx window object directly and make cross-thread calls to the GUI, e.g.:

>>> win = GetPlot(plot.id)
>>> wx.CallAfter(win.Hide)
>>> wx.CallAfter(win.Show)
>>> wx.CallAfter(win.SetSize, (256, 256))
>>> wx.CallAfter(win.Move, (256, 256))

There is no way at present to return a value from a cross-thread call.

Examples

Calling the program from the command line launches the GUI thread and then enters a loop where it creates a new wx 1d plot every 5 seconds, while the main Python program continues to run. The program exits after 10 windows are created:

25: ~/Projects/Python/AstroPy > tplots.py
callFn: thread=, args=(1,), winlist=[]
callFn: thread=, args=(2,), winlist=[]
callFn: thread=, args=(3,), winlist=[]
callFn: thread=, args=(4,), winlist=[]
callFn: thread=, args=(5,), winlist=[]
callFn: thread=, args=(6,), winlist=[<__main__.TPlotFrame; proxy of  >]
callFn: thread=, args=(7,), winlist=[<__main__.TPlotFrame; proxy of  >]
callFn: thread=, args=(8,), winlist=[<__main__.TPlotFrame; proxy of  >]
callFn: thread=, args=(9,), winlist=[<__main__.TPlotFrame; proxy of  >]
callFn: thread=, args=(10,), winlist=[<__main__.TPlotFrame; proxy of  >]
callFn: thread=, args=(11,), winlist=[<__main__.TPlotFrame; proxy of  >, <__main__.TPlotFrame; proxy of  >]
etc...

Plots can also be created from an interactive Python session by importing the module and calling NewPlots() and other functions. See the 11/19/07 entry of the Python worklog for a detailed example of using threaded plots from a Python terminal session:

> python
Python 2.5.1 (r251:54869, Apr 18 2007, 22:08:04) 
[GCC 4.0.1 (Apple Computer, Inc. build 5367)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from numpy import *
>>> from tplots import *
>>> ang = arange(500) / 499.0 * 10.0 * pi
>>> data = sin(ang)
>>> r = arange(500) / 499.0
>>> x = r * cos(ang)
>>> y = r * sin(ang)
>>> p1 = NewPlot('1d', outer(arange(10), data), 'Data')
>>> p2 = NewPlot('2d', concatenate((x, y)).reshape(2, 500).transpose(), 'XY data')
>>> p3 = NewPlot('im', outer(data, data), 'Image data')


©Sky Coyote 2007