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!

6
  • 1
    "all other browsers and windows will be affected" I don't know what this means Commented Mar 10, 2019 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 example. Or easier, just use the "Agg" backend without changing your code. Commented Mar 10, 2019 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. Commented Mar 10, 2019 at 22:13
  • 1
    Probably related: stackoverflow.com/q/44278369/2454357 Commented Mar 11, 2019 at 6:48
  • 2
    Thank you @ThomasKühn. I added the following lines and it seems working: import matplotlib and matplotlib.use('Agg') Commented Mar 11, 2019 at 17:51

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.