On Thu, Jul 5, 2012 at 11:15 AM, Fabrice Silva <silva@lma.cnrs-mrs.fr> wrote:

> At end of the outer loop, instead of closing the figure, you should
> call "remove()" for each plot element you made.  Essentially, as you
> loop over the inner loop, save the output of the plot() call to a
> list, and then when done with those plots, pop each element of that
> list and call "remove()" to take it out of the subplot.  This will let
> the subplot axes retain the properties you set earlier.

Instead of remove()'ing the graphical elements, you can also reuse them
if the kind of plots you intend to do is the same along the figure
for simple plots. See : http://paste.debian.net/177857/

I was close to getting the script run as you pasted. (One minor correction in your script is indexing L1 and L2, either L1[0] or L1, (comma) required in the assignments since grid.plot returns a list) The key here was "reuse" as you told.  Memory consumption almost drops to half comparing to the test_speed2.py script run.  Now I am down to ~1 minutes from about ~4 minutes execution times, which is indeed quite significant, provided that I experiment on 6 such 38 pages plots. 

nums = 2
I1 time run test_speed3.py
CPU times: user 8.19 s, sys: 0.07 s, total: 8.26 s
Wall time: 8.49 s 

I1 time run test_speed3.py
CPU times: user 78.84 s, sys: 0.19 s, total: 79.03 s
Wall time: 80.88 s

Thanks Fabrice for your feedback.