通过Matplotlib中的OO接口获取图形管理器 [英] Obtaining the figure manager via the OO interface in Matplotlib
问题描述
我希望能够获得已创建人物的fig_manager: 例如我可以使用pyplot界面来做到这一点:
I want to be able to get the figure_manager of a created figure: e.g. I can do it with the pyplot interface using:
from pylab import*
figure()
plot(arange(100))
mngr = get_current_fig_manager()
但是,如果我有几个数字怎么办:
However what if I have a few figures:
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上都没有自动完成关于人物实例的操作,方法/成员似乎都没有给我经理".
however, searching carefully through the figure API, http://matplotlib.org/api/figure_api.html, was not useful. Neither was auto-complete in my IDE on an instance of a figure, none of the methods / members seemed to give me a 'manager'.
那么我该怎么做?总的来说,我应该在哪里查看在OO接口中是否需要其模拟的pyplot方法?
So how do I do this and in general, where should I look if there is a pyplot method whose analogue I need in the OO interface?
PS:无论如何,get_current_fig_manager()返回哪种对象? 在调试器中,我得到:
PS: what kind of an object is returned by get_current_fig_manager() anyway? In the debugger i get:
type(get_current_fig_manager())
<type 'instance'>
听起来很神秘...
推荐答案
好问题.没错,文档并没有说出能够聘请经理或画布的任何内容.根据代码的经验,您的问题的答案是:
Good question. Your right, the docs don't say anything about being able to get the manager or the canvas. From experience of the code the answer to your question is:
>>> 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方法,并找到了以下代码:
The best place to find out about this stuff is by reading the code. In this case, I knew I wanted to get the canvas so that I could get hold of the figure manager, so I looked at the set_canvas method in figure.py
and found the following code:
def set_canvas(self, canvas):
"""
Set the canvas the contains the figure
ACCEPTS: a FigureCanvas instance
"""
self.canvas = canvas
从那里(因为没有get_canvas方法),我知道了画布的存储位置,可以直接访问它.
From there, (as there was no get_canvas method), I knew where the canvas was being stored and could access it directly.
HTH
这篇关于通过Matplotlib中的OO接口获取图形管理器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!