import matplotlib
matplotlib.use('Agg')
from mpl_toolkits.basemap import Basemap
import matplotlib.pyplot as plt
from numpy import array
x_min = 10 ; x_max = 21.
y_min = -20. ; y_max = -10.
lon_box = array ( [x_min, x_min, x_max, x_max, x_min])
lat_box = array ( [y_min, y_max, y_max, y_min, y_min])
# this example shows how to save a map background and
# reuse it in another figure.
# make sure we have all the same properties on all figs
figprops = dict(figsize=(8,6), dpi=100, facecolor='white')
# generate the first figure.
fig1 = plt.figure(1,**figprops)
ax1 = fig1.add_axes([0.1,0.1,.8,.8])
# create basemap instance, plot coastlines.
map = Basemap(projection='moll',lon_0=0)
map.drawcoastlines()
#map.drawmapboundary(fill_color='aqua')
map.fillcontinents(color='coral',lake_color='aqua')
ax3 = fig1.add_axes([0.1,0.75,0.25,0.25])
m2 = Basemap(projection='ortho',lon_0=6,lat_0=-12,ax=ax3)
(x,y) = m2 (lon_box, lat_box)
#m2.drawmapboundary(fill_color='#33339F')
m2.drawcoastlines(linewidth=0.1)
m2.fillcontinents(color='chocolate',lake_color='#33339F')
m2.plot ( x, y, '-r', lw=2)
fig1.canvas.draw()
background = fig1.canvas.copy_from_bbox(fig1.bbox)
fig1.savefig('figure1.png', dpi=100)
# generate the second figure, re-using the background
# from figure 1.
for i in xrange(5):
fig2 = plt.figure(2,frameon=False,**figprops)
ax2 = fig2.add_subplot(111, frameon=False, xticks=[], yticks=[])
#ax2 = fig2.add_axes ([0.1, 0.1, 0.8, 0.8])
# restore previous background.
fig2.canvas.restore_region(background)
# draw parallels and meridians on existing background.
map.drawparallels(range(-90,90,30))
map.drawmeridians(range(-180,180,60))
fig2.savefig('figure%d.png'%(i+2), dpi=100)