Ok, I tried your last suggestion and sure enough it worked.
But it turns out to solve only half of my problem. I'd like to be able to
restore the background (using the Agg backend) and then use it further, i.e.
plot on it, without it disappearing.

Elaborating on my real use case, what I want to do is to make a Basemap
instance, plot the coastlines and other things that are constant in time
(put marker on the maps for cities, watermark the figure, draw meridians and
parallels and so on) and save that as a background.
I would then restore this background and plot "the weather" (open and filled
contours, quivers and barbs) onto it. I typically have data for some dozens
of timesteps, so I'd be restoring the background many times. Each time I'd
plot a particular weather (corresponding to a particular timestep) onto the
background I'd save that figure, so in the end I have some dozens of
figures.
Sliding the slider beneath the pictures at  http://www.belgingur.is
http://www.belgingur.is  should give a good idea of what I mean (notice that
the website is also in English).

A somewhat simplified example of what I'd like to do (but it catches the
essence of it) follows.
The problem that arises is that when I try to plot onto the background it
disappears and only what I tried to plot onto it remains. So figure2.png
contains the coastline of Iceland while figure3.png contains only the
quivers, the coastline has disappeared.

import matplotlib
matplotlib.use('Agg')

import matplotlib.pyplot as plt
import numpy
from mpl_toolkits.basemap import Basemap

plt.close('all')

basemap = Basemap(llcrnrlat=62.8, 
                  llcrnrlon=-24.8, 
                  urcrnrlat=66.7,
                  urcrnrlon=-12.4,
                  lat_0=65.0, 
                  lon_0=-19.5)

# make sure we have all the same properties on all figs
figprops = dict(figsize=(8,6), dpi=100, facecolor='white')
fig1 = plt.figure(1, **figprops)

basemap.drawcoastlines()

fig1.canvas.draw()
background = fig1.canvas.copy_from_bbox(fig1.bbox)

# turn the frame off or it will overwrite the background
fig2 = plt.figure(2, frameon=False, **figprops)
fig2.canvas.restore_region(background)
fig2.savefig('figure2.png', dpi=100)

# turn the frame off or it will overwrite the background
fig3 = plt.figure(3, frameon=False, **figprops)
fig3.canvas.restore_region(background)
# create a lon-lat grid for plotting quivers
n, m = 10, 5
latitudes = numpy.resize(numpy.linspace(63.5, 65.0, n), (n, m))
longitudes = numpy.transpose(numpy.resize(numpy.linspace(-22, -14, n),(n,
m)))
x, y = basemap(longitudes, latitudes)
# create the u and v components of the quivers
u = numpy.resize([5], (n, m))
v = numpy.resize([5], (n, m))
# since keyword ax is not supplied the current axes instance is used, i.e.
the axes of fig3
basemap.quiver(x, y, u, v)
fig3.savefig('figure3.png', dpi=100)

Hrafnkell
-- 
View this message in context: 
http://www.nabble.com/Save-a-plot-background-tp20519596p20562028.html
Sent from the matplotlib - users mailing list archive at Nabble.com.


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