Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Obtaining the figure manager via the OO interface in Matplotlib

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

like image 700
alexandre iolov Avatar asked Oct 25 '12 09:10

alexandre iolov


1 Answers

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

like image 90
pelson Avatar answered Oct 26 '22 06:10

pelson