【问题标题】:Obtaining the figure manager via the OO interface in MatplotlibMatplotlib中通过OO接口获取图形管理器
【发布时间】:2012-10-15 11:15:48
【问题描述】:

我希望能够获取已创建图形的 figure_manager: 例如我可以使用 pyplot 界面来做到这一点:

from pylab import*
figure()    
plot(arange(100))
mngr = get_current_fig_manager()

但是,如果我有几个数字怎么办:

from pylab import *
fig0 = figure()
fig1 = figure()    
plot(arange(100))
mngr = fig0.get_manager() #DOES NOT WORK - no such method as Figure.get_manager()

但是,仔细搜索图形 API http://matplotlib.org/api/figure_api.html 并没有用处。在我的 IDE 中,也没有在图形实例上自动完成,似乎没有任何方法/成员给我一个“经理”。

那么我该怎么做?一般来说,如果在 OO 接口中有我需要类似的 pyplot 方法,我应该在哪里查看?

PS: get_current_fig_manager() 返回什么样的对象? 在调试器中我得到:

type(get_current_fig_manager())
<type 'instance'>

听起来很神秘……

【问题讨论】:

标签: matplotlib figure


【解决方案1】:

好问题。你的权利,文档没有说明能够获得经理或画布。根据代码经验,您的问题的答案是:

>>> import matplotlib.pyplot as plt
>>> a = plt.figure()
>>> b = plt.figure()

>>> a.canvas.manager
<matplotlib.backends.backend_tkagg.FigureManagerTkAgg instance at 0x1c3e170>
>>> b.canvas.manager
<matplotlib.backends.backend_tkagg.FigureManagerTkAgg instance at 0x1c42ef0>

了解这些内容的最佳途径是阅读代码。在这种情况下,我知道我想获取画布以便我可以掌握图形管理器,所以我查看了figure.py 中的 set_canvas 方法,发现以下代码:

def set_canvas(self, canvas):
    """
    Set the canvas the contains the figure

    ACCEPTS: a FigureCanvas instance
    """
    self.canvas = canvas

从那里,(因为没有 get_canvas 方法),我知道画布的存储位置并可以直接访问它。

HTH

【讨论】:

    猜你喜欢
    • 2019-12-04
    • 1970-01-01
    • 1970-01-01
    • 2018-08-23
    • 1970-01-01
    • 1970-01-01
    • 2018-12-16
    • 2021-02-12
    • 1970-01-01
    相关资源
    最近更新 更多