Search code examples
pythonpython-3.xmatplotlibinlinefigure

Closing figure show vs. imshow


Normally, I use following figure-closing pattern to prevent the error

%matplotlib inline

plt.figure()
// plot here what necessary
plt.show()
// figure is displayed inline
plt.close()

Replacing show with imshow, within for in loop only 1 last figure is displayed.

How to plot inline in loop with imshow, making sure that closing figures properly?


Solution

  • based on comment

    for x in y:
        plt.figure()
        plt.imshow(...)
        plt.show()
        plt.close()