简体   繁体   English

Jupyter Notebook和Matplotlib(运行时警告):关闭后继续显示图

[英]Jupyter notebook and matplotlib (runtime warning): continue to display plots after closing

I would like to use matplotlib.pyplot to display many images in my jupyter notebook. 我想使用matplotlib.pyplot在jupyter笔记本中显示许多图像。 However, when I try to display more than 20, a runtime warning appears, since images are kept in memory if not closed: 但是,当我尝试显示20个以上时,会出现运行时警告,因为如果未关闭,图像会保留在内存中:

RuntimeWarning: More than 20 figures have been opened. 运行时警告:已打开20多个数字。 Figures created through the pyplot interface ( matplotlib.pyplot.figure ) are retained until explicitly closed and may consume too much memory. 通过pyplot接口( matplotlib.pyplot.figure )创建的图形将保留到显式关闭,并且可能会占用过多内存。 (To control this warning, see the rcParam figure.max_open_warning). (要控制此警告,请参阅rcParam Figure.max_open_warning)。

I am using %matplotlib inline 我正在使用%matplotlib inline

If I close the plot plt.close() , the plots are not displayed. 如果我关闭绘图plt.close() ,则不会显示绘图。 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. 我如何才能关闭绘图并仍然在jupyter中查看该绘图,我需要同时查看许多图像:我正在使用一个循环,其中crop和value是两个图像。

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()

I think you would want to use plt.show() at the end of your loop. 我认为您可能希望在循环结束时使用plt.show()

%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. 这样就无需关闭任何东西。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM