如何关闭 show() 窗口但保持图形活着? [英] how to close a show() window but keep the figure alive?

查看:58
本文介绍了如何关闭 show() 窗口但保持图形活着?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我创建一个图形然后执行 plt.close() :

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()
fig2.show()

fig1 只会显示一次,因为 plt.close() 会破坏 fig1 引用的图形对象.我怎么能只关上窗户而又不破坏人物呢?

the fig1 will only show once, because the plt.close() will destroy the figure object referred by fig1. How can I only close the window without destroy the figure?

到目前为止,什么都没有真正起作用.在每个plt.figure()之后,将生成一个新的fig_manager.并将被放入plt实例的列表中.

So far nothing really works. after each plt.figure(), a new figure_manager is going to be generated. And will be put into a list in plt instance.

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

但是,在plt.close()之后,会弹出特定图形的figure_manager.

however, after plt.close(), the figure_manager of the specific figure will be pop out.

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

正如@John Sharp 提到的 plt._backend_mod.new_figure_manager_given_figure(plt.get_fignums()[-1]+1,fig1) 将为 fig1 创建一个新的 figure_manager.但是,它没有被添加到 plt 中.因此,在plt时无法控制这些fig_manager:

As @John Sharp mentioned plt._backend_mod.new_figure_manager_given_figure(plt.get_fignums()[-1]+1,fig1) will create a new figure_manager for the fig1. However, it has not been added to the plt. So it is impossible to control those figure_manager 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]

因此它不能被plt.close()关闭,除非直接调用fig_manager.destroy()

So it cannot be closed by plt.close(), except directly call figure_manager.destroy()

直接设置当前fm的建议会更糟:

The suggestion to set current fm directly will be worse:

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

乍一看,这似乎可行.但是,它将直接将fig2的fm更改为指向fig1,这会造成很多麻烦.

at the first glance, this seems work. However, it will directly change the fig2's fm to point to fig1, which will cause lots of trouble.

如果有任何办法,我们可以使pyplot注册手动生成的fm,这可能会起作用.到目前为止还没有运气.

If there is any way, we can make the pyplot to register manually generated fm, that may work. So far have no luck there.

推荐答案

由于该图仍被名称fig1引用,因此不会被破坏.您只需要为该图创建一个新的图管理器.一种方法是通过生成新的空白图形并手动将画布图形设置为 fig1 来获取新图形管理器:

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

或者,如果您一次显示两个图形而只想关闭一个特定图形,则可以使用 fm.destroy()方法关闭只显示该框架管理器引用的特定图形的窗口.

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.

这篇关于如何关闭 show() 窗口但保持图形活着?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆