IOError "Too many open files" when using savefig () in a loop

I have a little problem with matplotlib. When trying to save several digits in a loop a hundred times with savefig() , it finally ends with the following error:

 IOError: [Errno 24] Too many open files: 'test_421.png' 

I checked that after each save of the figure I close it, but it does not seem to be effective.

Here is a sample code to illustrate how I get this error:

 def displayFig(input, display, savePath): fig = plt.figure(figsize=(22, 5)) # add some subplots, axis and colorbars... if display: plt.show() else: plt.savefig(savePath, dpi = 100) plt.close(fig) for i, inp in enumerate(inpArray): savePath = 'test_%d.png' % i displayFig(inp, False, savePath) 

Why does this error occur? Maybe this is a multithreading problem or a memory leak?

+4
source share
1 answer

Try updating matplotlib version. I believe that from matplotlib v1.2.0 this has been reviewed (by this PR https://github.com/matplotlib/matplotlib/issues/1466/ ).

0
source

Source: https://habr.com/ru/post/1494444/


All Articles