Subclassing Figure and pylab.figure()

That seems to give me what I want, as long as I use the

    > GTKAgg backend. What should I do to have the same result
    > with another backend, without having to rewrite a
    > new_figure_manager each time ? Anyway, is it even the way
    > to go ?

Note that pylab basically has the same problem, as each backend
defines their own new_figure_manager function. The problem is not the
Figure, but the FigureCanvas, which is backend dependent. Take a look
at the "switch_backend" function in pylab

def switch_backend(newbackend):
    """
    Swtich the default backend to newbackend. This feature is
    EXPERIMENTAL, and is only expected to work switching to an image
    backend. Eg, if you have a bunch of PS scripts that you want to
    run from an interactive ipython session, yuo may want to switch to
    the PS backend before running them to avoid having a bunch of GUI
    windows popup. If you try to interactively switch from one GUI
    backend to another, you will explode.

    Calling this command will close all open windows.
    """
    close('all')
    global new_figure_manager, draw_if_interactive, show
    matplotlib.use(newbackend)
    reload(backends)
    from backends import new_figure_manager, draw_if_interactive, show

You could emulate this approach ....

JDH

John,
Thx for your answer. IMHO, the problem lies really with Figure, not Canvas.
I'd need the Canvas, whatever backend defines it, recognizes that the figure
is not a classical 'Figure' , but a subclass of it, with its own special
properties/methods.
I gonna think aloud for a minute

One way would be to add a 'subclass' option to every new_figure_manager,
so that the
`thisFig = Figure(**figargs)`
would be replaced by
`thisFig = subclass(**figargs)`
(with subclass set to Figure by default). A bit overkill, eh ?

In fact, as long as my special properties/methods can be accessed Figure
instance, I should be OK. What would be the better way to have the
properties/methods of a child recognized by its parent (revered
inheritance ?)

···

On Tuesday 20 June 2006 11:37, John Hunter wrote:

    > Folks,
    > I have a tailor-made subclass of Figure (say, MyFigure) that I'd
    > like to use interactively. Ideally, I'd like to have
    > pylab.figure() (or an equivalent) create a MyFigure instance,
    > instead of Figure.
    >
    > I wrote a myfigure() function based on pylab.figure(),
    > substituting the
    > -----
    > figManager = new_figure_manager(num, **figargs)
    > -----
    > by
    > -----
    > thisFig = MyFigure(*args, **kwargs)
    > canvas = FigureCanvasGTKAgg(thisFig)
    > figManager = FigureManagerGTKAgg(canvas, num)
    > ----
    >
    > That seems to give me what I want, as long as I use the
    > GTKAgg backend. What should I do to have the same result
    > with another backend, without having to rewrite a
    > new_figure_manager each time ? Anyway, is it even the way
    > to go ?

Note that pylab basically has the same problem, as each backend
defines their own new_figure_manager function. The problem is not the
Figure, but the FigureCanvas, which is backend dependent. Take a look
at the "switch_backend" function in pylab

def switch_backend(newbackend):
    """
    Calling this command will close all open windows.
    """
    close('all')
    global new_figure_manager, draw_if_interactive, show
    matplotlib.use(newbackend)
    reload(backends)
    from backends import new_figure_manager, draw_if_interactive, show

You could emulate this approach ....