> I spent some time working on it and came to the following conclusion:
> if the mpl figure is fully transparent, you see whatever is in the gui
> rendering buffer, which may be unintiialized memory.   In some sense,
> mpl is doing what is asked of it, making a fully transparent figure.
> Clearing the agg buffer is not enough if it is transparent -- you also
> have to init the canvas buffer.  The question is: with what?
>

I think what we may want is to let the the gui backend do the
compositing, i.e., showing a composite image of the background (of the
canvas widget) and what is in the agg buffer.
In recent version of GTK(2.8 and later), you can do it with cairo.

The attached is a test patch for the gtkagg backend. It includes a
small example which sets an image as a background of the canvas (see
the attached image).

I found this patch make the mpl quite slowed down in my unix box, but
fine in my macbook (my guess is it depends on whether cairo uses the
hardware acceleration or not).

Regards,

-JJ
Index: lib/matplotlib/backends/backend_agg.py
===================================================================
--- lib/matplotlib/backends/backend_agg.py	(revision 6360)
+++ lib/matplotlib/backends/backend_agg.py	(working copy)
@@ -280,6 +280,7 @@
         if __debug__: verbose.report('FigureCanvasAgg.draw', 'debug-annoying')
 
         self.renderer = self.get_renderer()
+        self.renderer.clear()
         self.figure.draw(self.renderer)
 
     def get_renderer(self):
Index: lib/matplotlib/backends/backend_gtkagg.py
===================================================================
--- lib/matplotlib/backends/backend_gtkagg.py	(revision 6360)
+++ lib/matplotlib/backends/backend_gtkagg.py	(working copy)
@@ -10,7 +10,7 @@
 from matplotlib.backends.backend_gtk import gtk, FigureManagerGTK, FigureCanvasGTK,\
      show, draw_if_interactive,\
      error_msg_gtk, NavigationToolbar, PIXELS_PER_INCH, backend_version, \
-     NavigationToolbar2GTK
+     NavigationToolbar2GTK, GTK_WIDGET_DRAWABLE
 from matplotlib.backends._gtkagg import agg_to_gtk_drawable
 
 
@@ -48,6 +48,11 @@
     filetypes = FigureCanvasGTK.filetypes.copy()
     filetypes.update(FigureCanvasAgg.filetypes)
 
+    def __init__(self, *kl, **kw):
+        FigureCanvasGTK.__init__(self, *kl, **kw)
+        FigureCanvasAgg.__init__(self, *kl, **kw)
+        self.set_double_buffered(True)
+
     def configure_event(self, widget, event=None):
 
         if DEBUG: print 'FigureCanvasGTKAgg.configure_event'
@@ -87,6 +92,42 @@
                            gtk.gdk.RGB_DITHER_NONE, 0, 0)
         if DEBUG: print 'FigureCanvasGTKAgg.render_figure done'
 
+
+    def _render_figure(self, pixmap, width, height):
+
+        FigureCanvasAgg.draw(self)
+
+        buf = self.buffer_rgba(0,0)
+        ren = self.get_renderer()
+        w = int(ren.width)
+        h = int(ren.height)
+
+        pixbuf = gtk.gdk.pixbuf_new_from_data(
+            buf, gtk.gdk.COLORSPACE_RGB,  True, 8, w, h, w*4)
+        self._pixbuf = pixbuf
+
+        if DEBUG: print 'FigureCanvasGTKAgg.render_figure done'
+
+    def expose_event(self, widget, event):
+        """Expose_event for all GTK backends. Should not be overridden.
+        """
+
+        if GTK_WIDGET_DRAWABLE(self):
+            if self._need_redraw:
+                x, y, w, h = self.allocation
+                self._render_figure(None, w, h)
+                self._need_redraw = False
+
+            x, y, w, h = event.area
+            cairo_ctx = self.window.cairo_create()
+            cairo_ctx.set_source_pixbuf(self._pixbuf, 0., 0.)
+            cairo_ctx.rectangle(x, y, w, h)
+            cairo_ctx.clip()
+            cairo_ctx.paint()
+            
+        return False  # finish event propagation?
+
+
     def blit(self, bbox=None):
         if DEBUG: print 'FigureCanvasGTKAgg.blit'
         if DEBUG: print 'FigureCanvasGTKAgg.blit', self._pixmap
Index: examples/user_interfaces/embedding_in_gtkagg_w_bg.py
===================================================================
--- examples/user_interfaces/embedding_in_gtkagg_w_bg.py	(revision 0)
+++ examples/user_interfaces/embedding_in_gtkagg_w_bg.py	(revision 0)
@@ -0,0 +1,54 @@
+#!/usr/bin/env python
+"""
+show how to add a matplotlib FigureCanvasGTK or FigureCanvasGTKAgg widget and
+a toolbar to a gtk.Window
+"""
+import gtk
+
+from matplotlib.figure import Figure
+from numpy import arange, sin, pi
+
+# uncomment to select /GTK/GTKAgg/GTKCairo
+#from matplotlib.backends.backend_gtk import FigureCanvasGTK as FigureCanvas
+from matplotlib.backends.backend_gtkagg import FigureCanvasGTKAgg as FigureCanvas
+#from matplotlib.backends.backend_gtkcairo import FigureCanvasGTKCairo as FigureCanvas
+
+# or NavigationToolbar for classic
+#from matplotlib.backends.backend_gtk import NavigationToolbar2GTK as NavigationToolbar
+from matplotlib.backends.backend_gtkagg import NavigationToolbar2GTKAgg as NavigationToolbar
+
+
+win = gtk.Window()
+win.connect("destroy", lambda x: gtk.main_quit())
+win.set_default_size(400,300)
+win.set_title("Embedding in GTK")
+
+vbox = gtk.VBox()
+win.add(vbox)
+
+fig = Figure(figsize=(5,4), dpi=100, frameon=False)
+ax = fig.add_subplot(111)
+ax.patch.set_visible(False)
+
+t = arange(0.0,3.0,0.01)
+s = sin(2*pi*t)
+
+ax.plot(t,s)
+
+
+canvas = FigureCanvas(fig)  # a gtk.DrawingArea
+vbox.pack_start(canvas)
+toolbar = NavigationToolbar(canvas, win)
+vbox.pack_start(toolbar, False, False)
+
+imagename = "../data/lena.jpg"
+pixbuf = gtk.gdk.pixbuf_new_from_file(imagename)
+pixmap, mask = pixbuf.render_pixmap_and_mask()
+
+style = canvas.get_style().copy()
+style.bg_pixmap[gtk.STATE_NORMAL]=pixmap
+canvas.set_style(style)
+
+
+win.show_all()
+gtk.main()

<<attachment: gtkagg_test.jpg>>

-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users

Reply via email to