plotting 100's of figures, mpl slows and consumes memory!

I have a script looping through and plotting 100's of figures. It runs fine,
but after the first few plots, the loop considerably slows down and the
memory usage keeps going up.

The script is quite complicated, so can't really paste it here, but I am
trying to pass figure instances around and I am trying to reuse the
axes/figures... but maybe someone could demonstrate how this is done
efficiently?

What do I need to do so that each successive plot is using the same instance
of axes rather than adding a new one... this is all I can imagine is
happening.

Thanks,
john

···

--
View this message in context: http://www.nabble.com/plotting-100's-of-figures%2C-mpl-slows-and-consumes-memory!-tp24543343p24543343.html
Sent from the matplotlib - users mailing list archive at Nabble.com.

You either need to close the figure in the loop, or reuse the same
figure and cla it, eg

for i in range(1000):
    fig = plt.figure()
    # plot something
    plt.close(fig)

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

···

On Fri, Jul 17, 2009 at 7:02 PM, John [H2O]<washakie@...287...> wrote:

I have a script looping through and plotting 100's of figures. It runs fine,
but after the first few plots, the loop considerably slows down and the
memory usage keeps going up.

The script is quite complicated, so can't really paste it here, but I am
trying to pass figure instances around and I am trying to reuse the
axes/figures... but maybe someone could demonstrate how this is done
efficiently?

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.