On Tue, May 26, 2009 at 7:39 AM, Michael Droettboom <md...@stsci.edu> wrote: > Does it help if you add a call to "plt.clf()" to the bottom of the loop? > > The pyplot interface keeps a reference around to every figure created > until they are destroyed so that it can be obtained again by number > (this is functionality inspired by matlab). Alternatively, you can use > the object-oriented interface to create the figure, which does not have > this behavior, e.g., replace > > fig = plt.figure() > > with > > from matplotlib import figure > fig = figure.Figure() > > If all this doesn't help, let me know and I'll look further. > > Cheers, > Mike > > iCy-fLaME wrote: >> I was trying to use matplotlib to plot a series of 2D images, but >> python was using up a large amount of RAM very quickly. I don't know >> matplotlib that well, so the chance are I am missing something, would >> appreciate it if anyone can point me to the right direction. >> >> I am using: >> Python 2.4.3 (#1, Jan 21 2009, 01:11:33) >> [GCC 4.1.2 20071124 (Red Hat 4.1.2-42)] on linux2 >> >> Example code to run in interpreter mode: >> >> ######################################## >> from numpy import zeros >> >> x = 1651 >> y = 452 >> page = zeros((x, y)).astype('float') >> >> import matplotlib >> matplotlib.use('Agg') >> import matplotlib.pyplot as plt >> >> for i in range(1000): >> fig = plt.figure() >> ax = fig.add_subplot(111) >> cax = ax.imshow(page, cmap=plt.cm.spectral_r, extent=(-44, 176, -30, >> 30), interpolation = 'bicubic', vmin = -0.003, vmax = 0.003) >> title = "Time = %(i)0.3es)" % {'i':i} >> ax.set_title(title,fontsize=14) >> >> fig.colorbar(cax, ticks=[-2e-3, -1e-3, 0, 1e-3, 2e-3], >> orientation='horizontal') >> >> fig.savefig('_tmp.' + str(i) + ".png", dpi=300)
This code creates 1000 different figures -- either reuse the same figure and clear it as Michael suggests fig = plt.figure(1) # by putting 1 here you reuse the same fig fig.clf() # and clear it or close the figure in the loop fig = plt.figure() # draw and save here plt.close(fig) JDH ------------------------------------------------------------------------------ Register Now for Creativity and Technology (CaT), June 3rd, NYC. CaT is a gathering of tech-side developers & brand creativity professionals. Meet the minds behind Google Creative Lab, Visual Complexity, Processing, & iPhoneDevCamp asthey present alongside digital heavyweights like Barbarian Group, R/GA, & Big Spaceship. http://www.creativitycat.com _______________________________________________ Matplotlib-users mailing list Matplotlib-users@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/matplotlib-users