How to close the show () window, but keep the figure alive?

if I create a shape, then plt.close ():

from matplotlib import pyplot as plt fig1 = plt.figure() fig2 = plt.figure() fig1.show() plt.close() fig1.show() fig2.show() 

fig1 will only be displayed once, since plt.close () will destroy the shape object shown in fig. 1. How can I close the window without destroying the figure?

So far, nothing really works. after each plt.figure () a new figure_manager will be created. And will be listed in the plt instance.

 >>> print plt.get_fignums() [1, 2] 

however, after plt.close (), the figure_mander of a particular drawing will appear.

 >>> print plt.get_fignums() [2] 

As @John Sharp mentioned plt._backend_mod.new_figure_manager_given_figure (plt.get_fignums () [- 1] + 1, fig1) will create a new fig_manager for fig1. However, it was not added to plt. Thus, it is not possible to control these manipulator figures while plt:

 >>> plt._backend_mod.new_figure_manager_given_figure(plt.get_fignums()[-1]+1,fig1) <matplotlib.backends.backend_tkagg.FigureManagerTkAgg instance at 0x2b0f680> >>> print plt.get_fignums() [2] >>> plt.new_figure_manager(1) <matplotlib.backends.backend_tkagg.FigureManagerTkAgg instance at 0x2b1e3f8> >>> plt.get_fignums() [2] 

Therefore, it cannot be closed by plt.close (), except for a direct call to figure_manager.destroy ()

The suggestion to set the current fm directly will be worse:

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

at first glance, it seems to work. However, it will directly modify fig2 fm to point to fig1, which will cause a lot of problems.

If there is any way, we can force pyplot to register manually created fm that may work. So far they’re out of luck.

+1
source share
1 answer

Since the name fig1 is still referenced in the figure, 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 shape manager by creating a new shape and manually setting the canvas figure fig1:

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

Once you do this, you can show and then close the shape with:

 fig1.show() plt.close() 

Alternatively, if you immediately showed two digits and wanted to close only one, instead of using plt.close() you can call the fm.destroy() method to close the window showing only the specific digit that this frame manager refers to.

+1
source

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


All Articles