Hi!

I'm writing a gtk application that uses matplotlib heavily for data 
visualisation, but all the time consuming redrawing of the figure canvases took 
away much of the usability. So I wrote a FigureCanvas that delays redraws until 
it hasn't seen any new resize_events for a short while. It works much more 
smoothly on ubuntulinux than on winxp (where many redraws slip through the 
seams and I'm not sure why that happens), but anyway - tell me what you think! 
Is this a good solution, or is there a smarter approach?

Cheers!
/Joel


import time

import gtk
import gobject
import pygtk
pygtk.require20()

import matplotlib
try:
    from matplotlib.backends.backend_gtkagg import FigureCanvasGTKAgg as FigureCanvas
    matplotlib.use('GTKAgg')
except:
    from matplotlib.backends.backend_gtk import FigureCanvasGTK as FigureCanvas
    matplotlib.use('GTK')


class DelayedRedrawCanvas(FigureCanvas):
     delay = 0.1
     def __init__(self, figure):
         FigureCanvas.__init__(self, figure)
         self.draw_time = 0.0
         self.draw_source = None

     def resize_event(self):
         print "resize_event"
         self.draw_time = time.time() + self.delay
         return FigureCanvas.resize_event(self)

     def delayed_expose_event(self, widget, event):
         class pseudoevent:
             area = event.area
         def timeout_callback():
             print "Contemplating delayed draw...",
             if time.time() < self.draw_time:
                 print "no. Will wait %.2f seconds more." % (self.draw_time - time.time())
                 return True
             print "yes! It's time!"
             FigureCanvas.expose_event(self, widget, pseudoevent)
             self.draw_source = None
         if self.draw_source:
             gobject.source_remove(self.draw_source)
         print "Updating redraw task"
         self.draw_source = gobject.timeout_add(int(500 * self.delay), timeout_callback)

     def expose_event(self, widget, event):
         print "expose_event",
         if self._need_redraw:
             print "(enqueueing redraw)"
             self.delayed_expose_event(widget, event)
         else:
             print "(no need to redraw, so exposing instantly)"
             FigureCanvas.expose_event(self, widget, event)

if __name__ == '__main__':
    window = gtk.Window(gtk.WINDOW_TOPLEVEL)
    window.connect('destroy', gtk.main_quit)
    figure = matplotlib.figure.Figure()
    canvas = DelayedRedrawCanvas(figure)
    figure.add_axes([0,0,1,1]).plot([1, 3, 2.2, 2, 4])
    window.add(canvas)
    window.show_all()
    gtk.main()
-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2005.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
_______________________________________________
Matplotlib-devel mailing list
Matplotlib-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-devel

Reply via email to