3

With a single script, I have to produce dozens of figures with matplotlib using spyder. I would like spyder to create figures while I can work with other browsers or windows without interruption. Here were the methods I tried. Let us say that there are 40 plots to be created. Each plot contains multiple subplots

(1) From How do I get interactive plots again in Spyder/IPython/matplotlib?. I used %matplotlib inline. The plots will be created inline at the end of the program. However, the usage of computation memory keeps accumulating. At some point, python will show an error message due to lack of memory and then crash. The message is like: "More than 20 figures have been opened. Figures created through the pyplot interface (matplotlib.pyplot.figure) are retained until explicitly closed and may consume too much memory."

(2) Then, I use plt.close() to close each figure after saving it. The memory issue is resolved. However, even if the figure is not shown, every time python close a figure and open another one, all other browsers and windows will be affected. If I am typing on a window, after the interruption, I have to click the window again in order to continue typing.

Previously, when I used MATLAB, I could simply specify h=figure and then set(h,'visible','off') to avoid this issue. I wonder if I can do similar things with python.

Thank you for the help!

llin
  • 59
  • 3
  • 1
    "all other browsers and windows will be affected" I don't know what this means – roganjosh Mar 10 '19 at 22:08
  • 1
    Do you need a window to show your figures? If not, there isn't even any need to use `pyplot` at all. See [canvasagg](https://matplotlib.org/gallery/user_interfaces/canvasagg.html) example. Or easier, just use the `"Agg"` backend without changing your code. – ImportanceOfBeingErnest Mar 10 '19 at 22:13
  • I mean that if I am typing on a browser, any action of python will make itself the current working window and make my browser not current one. Thus, I have to click again my browser in order to continue typing. – llin Mar 10 '19 at 22:13
  • 1
    Probably related: https://stackoverflow.com/q/44278369/2454357 – Thomas Kühn Mar 11 '19 at 06:48
  • 2
    Thank you @ThomasKühn. I added the following lines and it seems working: `import matplotlib` and `matplotlib.use('Agg')` – llin Mar 11 '19 at 17:51
  • Thank you @ImportanceOfBeingErnest. I cannot add two commenters in a comment... – llin Mar 11 '19 at 17:53

0 Answers0