On 3/28/07, Ken McIvor <mcivor@...> wrote:
> On Mar 27, 2007, at 12:35 PM, Antonino Ingargiola wrote:
> 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).
Many thanks so far...
PS: Here it is the script:
from time import sleep
from random import randint
from pylab import *
N = 5
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()
global image, color_bar
image = imshow(data, interpolation='nearest', origin='upper',
color_bar = colorbar()
if __name__ == '__main__':