Obtaining the figure manager via the OO interface in Matplotlib

寵の児 提交于 2019-12-10 15:44:34

问题


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

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'.

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: 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'>

which sounds pretty mysterious...


回答1:


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>

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

From there, (as there was no get_canvas method), I knew where the canvas was being stored and could access it directly.

HTH



来源:https://stackoverflow.com/questions/13065753/obtaining-the-figure-manager-via-the-oo-interface-in-matplotlib

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!