1
votes

I would like to use matplotlib.pyplot to display many images in my jupyter notebook. However, when I try to display more than 20, a runtime warning appears, since images are kept in memory if not closed:

RuntimeWarning: 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. (To control this warning, see the rcParam figure.max_open_warning).

I am using %matplotlib inline

If I close the plot plt.close(), the plots are not displayed. How can I can close the plot and still look at the plot in jupyter, I need to look at many images simultanously: I am using a loop where crop and value are two images.

plt.figure()
f, axarr = plt.subplots(1,2)
plt.title('Image: ' + str(key))
plt.gray()
axarr[0].imshow(crop)
axarr[1].imshow(value)
plt.close()
1
It's a warning, not something that actually stops things being displayed? - roganjosh

1 Answers

3
votes

I think you would want to use plt.show() at the end of your loop.

%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np

for i in range(25):
    fig, ax_arr = plt.subplots(1,2)
    ax_arr[0].imshow(np.random.randn(10,10))
    ax_arr[1].imshow(np.random.randn(10,10))
    plt.show()

There is then no need to close anything.