Re: [Matplotlib-users] no canvas reinitialization between calls to savefig
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())
Re: [Matplotlib-users] no canvas reinitialization between calls to savefig
I can reproduce Thomas' problem with Agg backend. It does not happen if frame_on is True. And I guess Thomas' guess in the first email might be right. I had a quick look at the mpl source and I don't think draw(), clf() or savefig() try to clear the canvas. The problem is not visible if frame_on is True. Thomas, Put following line after the savefig() function fig.canvas._lastKey = None and see if it solves the problem (savefig command in your original email is outside the for loop, I guess this was a typo). The Agg backend has a clear() method which fills the canvas with (1, 1, 1, 0). I tried to put this method in a few different places. It worked for savefig(), but I couldn't get it work in the interactive backend (GtkAgg). Regards, -JJ On Fri, Oct 31, 2008 at 6:35 AM, Thomas Pfaff [EMAIL PROTECTED] wrote: Hi, Thanks for the quick reply. My OS is Windows XP with Service Pack 3 I'm using an easy-install .egg distribution. Whether I downloaded it somewhere or simply installed it using easy-install, I don't remember. I have another version of matplotlib (0.91.2) installed, which does not seem to be used. This one was installed using the Windows installer from the matplotlib homepage. This should not interfere, should it? Running the script with --verbose-helpful gives the following results (I removed parts of the path information) $HOME=\pfaff CONFIGDIR=\pfaff\.matplotlib matplotlib data path \python25\lib\site-packages\matplotlib-0.98.3-py2.5-win32.egg\matplotlib\mpl -data loaded rc file \python25\lib\site-packages\matplotlib-0.98.3-py2.5-win32.egg\matplotlib\mpl -data\matplotlibrc matplotlib version 0.98.3 verbose.level helpful interactive is False units is False platform is win32 Using fontManager instance from \pfaff\.matplotlib\fontManager.cache backend TkAgg version 8.4 Now that I think of it: In the IPython reference, I read about possible problems with tk. When starting IPython I use -pylab -tk because otherwise I can only do one plot and the plotting window hangs once I issue another plotting command. As I didn't find where to tell matplotlib to behave like these two switches were set, I get by with constantly restarting my interpreter when working interactively. Could this be part of the problem? Cheers, Thomas -Original Message- From: Michael Droettboom [mailto:[EMAIL PROTECTED] Sent: Thursday, October 30, 2008 2:21 PM To: Thomas Pfaff Cc: matplotlib-users@lists.sourceforge.net Subject: Re: [Matplotlib-users] no canvas reinitialization between calls to savefig I can't reproduce this here with SVN trunk. I get what you expect. Can you provide the information outlined here: http://matplotlib.sourceforge.net/faq/troubleshooting_faq.html#report-a- problem as well as the backend you are using? Cheers, Mike - 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=100url=/ ___ Matplotlib-users mailing list Matplotlib-users@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/matplotlib-users - 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=100url=/ ___ Matplotlib-users mailing list Matplotlib-users@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/matplotlib-users
Re: [Matplotlib-users] no canvas reinitialization between calls to savefig
Hi, Thanks for the quick reply. My OS is Windows XP with Service Pack 3 I'm using an easy-install .egg distribution. Whether I downloaded it somewhere or simply installed it using easy-install, I don't remember. I have another version of matplotlib (0.91.2) installed, which does not seem to be used. This one was installed using the Windows installer from the matplotlib homepage. This should not interfere, should it? Running the script with --verbose-helpful gives the following results (I removed parts of the path information) $HOME=\pfaff CONFIGDIR=\pfaff\.matplotlib matplotlib data path \python25\lib\site-packages\matplotlib-0.98.3-py2.5-win32.egg\matplotlib\mpl -data loaded rc file \python25\lib\site-packages\matplotlib-0.98.3-py2.5-win32.egg\matplotlib\mpl -data\matplotlibrc matplotlib version 0.98.3 verbose.level helpful interactive is False units is False platform is win32 Using fontManager instance from \pfaff\.matplotlib\fontManager.cache backend TkAgg version 8.4 Now that I think of it: In the IPython reference, I read about possible problems with tk. When starting IPython I use -pylab -tk because otherwise I can only do one plot and the plotting window hangs once I issue another plotting command. As I didn't find where to tell matplotlib to behave like these two switches were set, I get by with constantly restarting my interpreter when working interactively. Could this be part of the problem? Cheers, Thomas -Original Message- From: Michael Droettboom [mailto:[EMAIL PROTECTED] Sent: Thursday, October 30, 2008 2:21 PM To: Thomas Pfaff Cc: matplotlib-users@lists.sourceforge.net Subject: Re: [Matplotlib-users] no canvas reinitialization between calls to savefig I can't reproduce this here with SVN trunk. I get what you expect. Can you provide the information outlined here: http://matplotlib.sourceforge.net/faq/troubleshooting_faq.html#report-a- problem as well as the backend you are using? Cheers, Mike - 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=100url=/ ___ Matplotlib-users mailing list Matplotlib-users@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/matplotlib-users
Re: [Matplotlib-users] no canvas reinitialization between calls to savefig
On Fri, Oct 31, 2008 at 6:43 AM, Jae-Joon Lee [EMAIL PROTECTED] wrote: The Agg backend has a clear() method which fills the canvas with (1, 1, 1, 0). I tried to put this method in a few different places. It worked for savefig(), but I couldn't get it work in the interactive backend (GtkAgg). I think what you are seeing is the problem I described here http://www.nabble.com/gtkagg-pixel-buffer-bug-td18051692.html#a18051692 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? JDH - 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=100url=/ ___ Matplotlib-users mailing list Matplotlib-users@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/matplotlib-users
Re: [Matplotlib-users] no canvas reinitialization between calls to savefig
Hi Jae-Joon, yes, that solved it. Thank you very much. The savefig command should have been inside the for-loop. Thomas -Original Message- From: Jae-Joon Lee [mailto:[EMAIL PROTECTED] Sent: Friday, October 31, 2008 12:43 PM To: Thomas Pfaff Cc: Michael Droettboom; matplotlib-users@lists.sourceforge.net Subject: Re: [Matplotlib-users] no canvas reinitialization between calls to savefig I can reproduce Thomas' problem with Agg backend. It does not happen if frame_on is True. And I guess Thomas' guess in the first email might be right. I had a quick look at the mpl source and I don't think draw(), clf() or savefig() try to clear the canvas. The problem is not visible if frame_on is True. Thomas, Put following line after the savefig() function fig.canvas._lastKey = None and see if it solves the problem (savefig command in your original email is outside the for loop, I guess this was a typo). The Agg backend has a clear() method which fills the canvas with (1, 1, 1, 0). I tried to put this method in a few different places. It worked for savefig(), but I couldn't get it work in the interactive backend (GtkAgg). Regards, - 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=100url=/ ___ Matplotlib-users mailing list Matplotlib-users@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/matplotlib-users
Re: [Matplotlib-users] no canvas reinitialization between calls to savefig
I can't reproduce this here with SVN trunk. I get what you expect. Can you provide the information outlined here: http://matplotlib.sourceforge.net/faq/troubleshooting_faq.html#report-a-problem as well as the backend you are using? Cheers, Mike Thomas Pfaff wrote: Hello, I'm having a problem reusing a figure with the savefig command. I want to generate image timeseries with GoogleEarth displaying weather radar data, so my plot geometry is the same always and only the color of the patches which represent measurements changes over time. Now I want those portions of the image, where no precipitation occurs to be transparent so I was thinking about setting the visibility of those Polygons to False and that would be it. Unfortunately savefig seems to reuse the image it created before when called a second time. I wrote this example code. ## #start #set some image parameters figsize = (1.,1.) dpi = 300 rect = [0.,0.,1.,1.] #get figure and axes objects fig = plt.figure(figsize=figsize, dpi=dpi, frameon=False) ax = fig.add_axes(rect, frameon=False) ax.set_aspect(1.0) #create two polygons, one filling the left half of the image # one the right half poly1 = Polygon(([0,0],[0.5,0],[0.5,1],[0,1],[0,0]), fill=True) ax.add_patch(poly1) poly2 = Polygon(([0.5,0],[1,0],[1,1],[0.5,1],[0.5,0]), fill=True) ax.add_patch(poly2) # basic colors black, red, green, blue colors=['#00','#ff','#00ff00','#ff'] # switch for the left polygon values =[-1, 1, -1, 1] for value, color in zip(values, colors): if value 0: poly1.set_visible(False) else: poly1.set_visible(True) poly1.set_facecolor(color) poly2.set_facecolor(color) plt.savefig('test'+color[1:]+'.png',dpi=dpi,format='png',transparent=True) # end ## Now, what I expect and what I get is: Test00.png: black on the right side, transparent with tick marks on the left - this is what I get. Testff.png: red on both sides with tick marks visible - This is what I get. Test00ff00.png: green on the right side, transparent on the left. What I get is green on the right and red on the left. So the red from the previous patch has been retained Testff.png: blue on both sides, which is what I get as well. So apparently the plot is updated, but only in places where something is actually drawn. The rest remains unchanged and so transparency is not restored. I tried using clf, cla, deleting objects. In all cases savefig just always produced the first image over and over, most probably because nothing new was drawn. I even tried to remove fig's canvas object, hoping that the library might create a new one, but that only gave me a traceback. Any ideas what I'm doing wrong or how I could solve this problem? Thank you very much in advance, Thomas -- Michael Droettboom Science Software Branch Operations and Engineering Division Space Telescope Science Institute Operated by AURA for NASA - 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=100url=/ ___ Matplotlib-users mailing list Matplotlib-users@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/matplotlib-users