I think the savefig() command calls draw() internally, doesn't it?
So, I guess the restore_region() command comes before the draw() call,
i.e., it has no effect for the saved figure.
One way I can think of is to save the agg buffer without redrawing it.
It seems work.
-----------------------
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
fig1 = plt.figure(1)
ax1 = fig1.add_subplot(111)
ax1.grid()
fig1.canvas.draw()
background = fig1.canvas.copy_from_bbox(ax1.bbox)
fig1.savefig('figure1.png')
from matplotlib import _png
from matplotlib.cbook import is_string_like
def print_png(fig, filename_or_obj):
canvas = fig.canvas
renderer = canvas.get_renderer()
if is_string_like(filename_or_obj):
filename_or_obj = file(filename_or_obj, 'wb')
_png.write_png(renderer._renderer.buffer_rgba(0, 0),
renderer.width, renderer.height,
filename_or_obj, canvas.figure.dpi)
fig1.clf()
fig1.canvas.restore_region(background)
print_png(fig1, 'figure2.png')
---------------------------
Somehow, the copy_from_bbox() needs to be called before the savefig().
This is just a quick hacky solution.
But I think it would be nice if we have this kind of functionality in
the mpl, ideally with zorder support. For example,
> ax1.add_background(background, zroder=2)
And, restore_region(background) is called within the draw() method
(with correct zorder).
Just a thought.
IHTH,
-JJ
On Mon, Nov 17, 2008 at 12:37 PM, John Hunter <[EMAIL PROTECTED]> wrote:
> On Mon, Nov 17, 2008 at 8:05 AM, Hrafnkell Pálsson <[EMAIL PROTECTED]> wrote:
>>
>> Hi
>>
>> I tried you suggestions but it didn't work out for me.
>> In the following code I try to save the axes and the grid from figure1 into
>> buffer and then restore it on figure2 but figure2.png turns out to be of an
>> empty canvas.
>>
>> #!/usr/bin/env /opt/python/bin/python
>> # encoding: utf-8
>> import matplotlib
>> if not matplotlib.get_backend()=='agg':
>> matplotlib.use('Agg')
>> import pylab
>>
>> figure1 = pylab.figure(1)
>> axes1 = pylab.gca()
>> axes1.grid()
>> canvas1 = axes1.figure.canvas
>> background = canvas1.copy_from_bbox(axes1.bbox)
>> figure1.savefig('figure1.png')
>
> The only problem I could see with your code is that you need to force
> a draw before copying the background. But when I added the draw, and
> then further simplified to reuse the same canvas, I still am not
> seeing the restored region. I will need to do further digging to see
> what is going wrong, but the problem appears both on the maintenance
> branch and the trunk. Here is my modified test script:
>
> import matplotlib
> matplotlib.use('Agg')
>
> import matplotlib.pyplot as plt
>
> fig1 = plt.figure(1)
> ax1 = fig1.add_subplot(111)
> ax1.grid()
> fig1.canvas.draw()
> fig1.savefig('figure1.png')
> background = fig1.canvas.copy_from_bbox(ax1.bbox)
>
> fig1.clf()
>
>
> #figure2 = pylab.figure(2)
> #canvas2 = figure2.canvas
> fig1.canvas.restore_region(background)
> fig1.savefig('figure2.png')
>
>
> 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=100&url=/
> _______________________________________________
> Matplotlib-users mailing list
> [email protected]
> 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=100&url=/
_______________________________________________
Matplotlib-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/matplotlib-users