Below is what I am trying to do, perhaps I am doing something wrong
somewhere?
John Hunter-4 wrote:
or explicitly reuse the same fig by giving a figure number and clearing
it:
for i in range(1000):
fig = plt.figure(1)
# plot something
fig.cla()
JDH
My Program is below. NOTE: The kwargs bit is before I realized how much
easier it is just to assign defaults in the definition... (haven't changed
it yet).
The main thing I am using is FIGURE, which is a list containing the figure,
basemap instance, and the axes. I 'pass' these around trying to reuse them
as you show above.
In the main loop of the program I pass the FIGURE list to three calls to
plot_track. I try to reuse all the instances (I found creating the basemap
instance to be very slow). At the end of each loop in the mainloop I call:
FIGURE[2].cla()
Which I understand to clear the axes. Maybe I should also be calling:
FIGURE[0].cla()
??
Thank you in advance!
def plot_track(lon,lat,**kwargs):
""" Plot an longitude,latitude course over a basemap """
if 'region' in kwargs.keys():
region = kwargs['region']
else:
region = 1
if 'figname' in kwargs.keys():
figname = kwargs['figname']
else:
figname = None
if 'FIGURE' in kwargs.keys():
FIGURE = kwargs['FIGURE']
else:
FIGURE = [None,None,None]
if 'overlay' in kwargs.keys():
overlay = kwargs['overlay']
else:
overlay = 0
if 'zlevel' in kwargs.keys():
zlevel = kwargs['zlevel']
else:
zlevel = None
if 'zsize' in kwargs.keys():
zsize = kwargs['zsize']
else:
zsize = np.ones(len(lon))
if 'base' in kwargs.keys():
base = kwargs['base']
else:
base = 1
## Extract plotting kwargs, just makes a dict I can pass to basemap plot
command
plot_kwargs = set_plotkwargs(kwargs)
##Get fig if exists
fig=FIGURE[0]
m=FIGURE[1]
az=FIGURE[2]
nullfmt = NullFormatter()
if fig==None:
# get a basemap from Basemap passing the region info.
exec("fig,m=get_base%s(region=region,coords=(lon,lat))"%base)
if az==None:
ax=plt.gca()
pos = ax.get_position()
l, b, w, h = getattr(pos, 'bounds', pos)
az = fig.add_axes([l,b,w,h],frameon=False)
if az!=None and overlay==0:
az.cla()
plt.axes(az)
#PRINT TRACK
cx,cy = m(lon,lat)
if zlevel!=None:
c=m.scatter(cx,cy,zsize,zlevel,cmap=cm.spectral,marker=marker,faceted=False,zorder=10,alpha=0.35)
#m.scatter has no color bar, so create a ghost 'scatter' instance:
ax=plt.gca()
plt.draw()
pos = ax.get_position()
l, b, w, h = getattr(pos, 'bounds', pos)
jnkax = fig.add_axes([l,b,w,h],frameon=False)
axes(jnkax)
plt.figure();
plt.scatter(cx,cy,zsize,zlevel,cmap=cm.spectral,marker=marker,faceted=False,zorder=10,alpha=0.75)
plt.figure(fig.number);
cax = plt.axes([l+w+0.03, b, 0.02, h])
plt.colorbar(cax=cax) # draw colorbar
#delete the ghost instance
plt.close(2);
#make the ax axes active
plt.axes(ax);
else:
m.plot(cx,cy,**plot_kwargs)
plt.axes(az)
az.xaxis.set_major_formatter( nullfmt )
az.yaxis.set_major_formatter( nullfmt )
plt.setp(az, xticks=,yticks=)
az.axesPatch.set_alpha(0.0)
if figname:
savefig(figname)
FIGURE=[fig,m,az]
return FIGURE
···
--
View this message in context: http://www.nabble.com/plotting-100's-of-figures%2C-mpl-slows-and-consumes-memory!-tp24543343p24546109.html
Sent from the matplotlib - users mailing list archive at Nabble.com.