how to close a show() window but keep the figure alive?

前端 未结 1 1043
粉色の甜心
粉色の甜心 2021-01-27 04:59

if I create a figure then do plt.close() :

from matplotlib import pyplot as plt

fig1 = plt.figure()
fig2 = plt.figure()

fig1.show()
plt.close()
fig1.show()
fig         


        
相关标签:
1条回答
  • 2021-01-27 05:29

    Since the Figure is still referenced by the name fig1 it is not destroyed. You just need to create a new figure manager for the figure. One way to do this is to get a new figure manager by generating a new blank figure and manually set the canvas figure to be fig1:

    plt.figure()
    fm = plt.get_current_fig_manager()
    fm.canvas.figure = fig1
    fig1.canvas = fm.canvas
    

    Once you've done this you can show and then close the figure with:

    fig1.show()
    plt.close()
    

    Alternatively, if you were showing two figures at once and only wanted to close a particular one, instead of using plt.close() you can call the fm.destroy() method to close the window showing only the particular figure referenced by that frame manager.

    0 讨论(0)
提交回复
热议问题