no canvas reinitialization between calls to savefig

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=['#000000','#ff0000','#00ff00','#0000ff']
# 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:

Test000000.png: black on the right side, transparent with tick marks on the
left - this is what I get.

Testff0000.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

Test0000ff.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

--
Using Python2.5, matplotlib 0.98.3, numpy 1.1.1, matplotlib.basemap 0.99.1
On WindowsXP SP3, Pentium4 DualCore 3.4GHz, 3GB RAM

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=['#000000','#ff0000','#00ff00','#0000ff']
# 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:

Test000000.png: black on the right side, transparent with tick marks on the
left - this is what I get.

Testff0000.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

Test0000ff.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

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:mdroe@…86…]
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

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 <yael@...361...> 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:mdroe@…86…]
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=100&url=/
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users

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

···

On Fri, Oct 31, 2008 at 6:43 AM, Jae-Joon Lee <lee.j.joon@...287...> 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).

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:lee.j.joon@…287…]
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,

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

gtkagg.patch (4.59 KB)

gtkagg_test.jpg