On 3/28/07, Ken McIvor <[EMAIL PROTECTED]> wrote:
> On Mar 27, 2007, at 12:35 PM, Antonino Ingargiola wrote:
[cut]
> You should probably do the acquisition asynchronously by running it
> in a separate thread.  That thread would read in the data one point
> at a time, perform any pre-processing, and post the results to a
> place that's shared between it and the main plotting thread.  The
> main thread would periodically check and see if the shared data has
> changed and redraw the plot if needed.  I'm not sure how hard this is
> to do in a reasonable way in pylab, but I've used this approach
> before in wxPython GUIs.

That's exactly what I'd like to do. The problem is that if I run
gtk.main() (the gtk main  GUI loop) in a separate thread the program
stops until I do something on the GUI (for example passing the mouse
on it). My understanding so far is the following. When the function
that execute gtk.main() is executed, python doesn't switch thread
until either 100 bytecode instructions are executed or since an I/O
(potentially) blocking operation in executed. When I'm doing nothing
on the  GUI application, neither 100 byte code instructions are
executed in the thread neither an I/O call is performed, so the whole
program (including the *other* threads) stalls.

Basically, I don't know which is the right way to put a Gtk GUI in
background, while another thread get the data asynchronously.

BTW, the timer idea is good and eliminates the need to call
asynchronously the GUI thread to update the plot, which seems (the
latter case) not very simple to do.

> > Furthermore, this function plot the new data above the
> > old one, so the plot becomes slower and slower while the acquisition
> > goes on. If I uncommented the cla() line, I get a plot that is blank
> > most of the time and that shows the data only for a fraction of second
> > while the new plot is performed.
> `
> You might want to consider create a mock data source that generates a
> stream of values from some pre-collected data or Python's "random"
> module.  That would let you work on debugging the plotting end of
> things first.  It would also make it easier for you to share your
> code with the list.

Thanks to your suggestion to use the image.set_data method I've
created a simplified script (pasted at the end) that demonstrate how
to do live-update while acquiring (jep!).

The last think I'm not yet able to do is to update the colorbar to
autoscale with the new incoming data. The the script that follows
tries to update the colorbar too but it does not work (on matplotlib
0.87 at least).

Any hints?

Many thanks so far...

Ciao,

  ~ Antono


PS: Here it is the script:

from time import sleep
from random import randint
from pylab import *

N = 5

def fake_data_generator():
    sleep(randint(1,3)/10.0)
    return rand()*100

def data_aquisition_loop():
    data = zeros((N,N))
    for j in range(N):
        for i in range(N):
            print ' - Acquiring pixel (',i,',',j,') ... '
            data[j,i] = fake_data_generator()

            plot_update(data)
    return data

def setup_plot(data):
    global image, color_bar
    title('Scanning Data')
    image = imshow(data, interpolation='nearest', origin='upper',
            extent=[0,N,N,0])
    color_bar = colorbar()

def plot_update(data):
    image.set_data(data)
    color_bar.set_array(data)
    color_bar.autoscale()
    draw()

if __name__ == '__main__':

    ion()
    setup_plot(rand(N*N).reshape(N,N))
    data_aquisition_loop()

-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys-and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users

Reply via email to