3

I write a number of plots to a pdf with a loop like the following. It works but there are two very annoying issues,

1) When the loop runs, i see a lot of windows ('Figure 1') popped up. I think the command plt.close(fig) does not work as intended. This' really annoying because I might be doing something else when it runs and those pop-ups block my view to the other tasks.

2) Probably related to 1), memory usage goes up dramatically. In my real script, plotting something like 50 pages of graphs eats up > 32 Gb of ram. How could that be?!

with PdfPages('Manyplots.pdf') as pdf:
  for j in xrange(100):
    fig = plt.figure(1, figsize=(5,5))
    for fr in xrange(9):
      pp = fig.add_subplot(3,3,fr+1)
      pp.imshow(x, cmap=plt.cm.gray)
    pdf.savefig()
    plt.close(fig)

My questions are

1) any way to close a figure after the plot is done?

2) better still, how to suppress blank Figure pop-up since it should really be writing to an external file in the background,

3) any better way to save a series of plots to multiple pages of PDF?

horaceT
  • 621
  • 13
  • 26
  • For 1 and 2, can you just remove the imshow call? – Ric Dec 14 '15 at 20:15
  • not sure what you mean? pls elaborate. – horaceT Dec 14 '15 at 20:17
  • What version of matplotlib are you using? And what OS? I cannot reproduce this using matplotlib 1.5 on Ubuntu 14.04. – wflynny Dec 14 '15 at 20:23
  • ubuntu 14.04, matplotlib 1.4.2, python 2.7.6 – horaceT Dec 14 '15 at 20:26
  • Are you running this through a script or interactively? In your matplotlibrc file, do you see `interactive : True` (towards the top, below backend)? – wflynny Dec 14 '15 at 20:34
  • i tried both, one in a script which i run from command line, and then i copied over to run in spyder. – horaceT Dec 14 '15 at 21:32
  • in my /etc/matplotlibrc, 'interactive' is commented out. – horaceT Dec 14 '15 at 21:35
  • i just set 'interactive:False' and same pop-ups. – horaceT Dec 14 '15 at 21:43
  • @wflynny: is my issue really related to version 1.4.2? Can you confirm that you really don't see this problme in 1.5? – horaceT Dec 17 '15 at 03:10
  • @horaceT I don't see it in 1.5, but I can't confirm that upgrading to 1.5 will solve your problem. There are a lot of variables here. That said, I've used `PdfPages` often in the last few years and never remember seeing this bug. Do you have local matplotlibrc file (~/.config/matplotlib/matplotlibrc)? It may also help to see the entire script you're running just to check there isn't a `plt.ion()` somewhere. – wflynny Dec 17 '15 at 04:40

1 Answers1

0

Found the cause of the problem. My main script includes an import of someone's utility script, which imports pyplot and has an extra line,

import matplotlib.pyplot as plt
plt.ion()

When plt.ion is commented out, the popups are gone.

horaceT
  • 621
  • 13
  • 26