Subclassing Figure and pylab.figure()

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

I modified pylab (and every backend, damn there are a lot) to support
this feature. Now you can pass a FigureClass kwarg to the pylab
figure function. With minimal extra work, we could support defaults
so you don't have to explicitly pass it. But before I go ahead with
this, take a look and see if this is more like what you had in mind.

from pylab import figure, show, nx
from matplotlib.figure import Figure

class MyFigure(Figure):
    def __init__(self, *args, **kwargs):
        """
        custom kwarg figtitle is a figure title
        """
        figtitle = kwargs.pop('figtitle', 'hi mom')
        Figure.__init__(self, *args, **kwargs)
        self.text(0.5, 0.95, figtitle, ha='center')

fig = figure(FigureClass=MyFigure, figtitle='my title')
ax = fig.add_subplot(111)
ax.plot([1,2,3])
fig.savefig('test.png')
fig.savefig('test.ps')
show()

John

I modified pylab (and every backend, damn there are a lot) to support
this feature.

Wow, impressive ! Thx a lot

Now you can pass a FigureClass kwarg to the pylab
figure function. With minimal extra work, we could support defaults
so you don't have to explicitly pass it. But before I go ahead with
this, take a look and see if this is more like what you had in mind.

Yep indeed, that's pretty much what I was considering. And parsing the
MyFigure arguments through keywords seems the easiest, I'll keep that in mind
(I used optional arguments *args, as they're not used in Figure, but that's
trickier to keep track of).

Now, till we (you ;)) are it, what about updating Figure.add_subplot() to call
user-defined subplots ? I wrote something along the lines below for my own
needs, but that might be a useful addition...

Initial add_subplot
add_subplot(self, *args, **kwargs):
  ...
        if isinstance(args[0], Subplot) or isinstance(args, PolarSubplot):
            a = args[0]
            a.set_figure(self)
        else:
            ispolar = popd(kwargs, 'polar', False)
            if ispolar:
                a = PolarSubplot(self, *args, **kwargs)
            else:
                a = Subplot(self, *args, **kwargs)
  ...

Modified add_subplot
add_generic_subplot(figure, subplotclass, *args, **kwargs):
  ...
        if isinstance(args[0], Subplot):
            a = args[0]
            a.set_figure(self)
        else:
            a = subplotclass(self, *args, **kwargs)
  ...

That way, if you need a PolarSubplot, you can still get it. Well, we'd have to
make PolarSubplot a subclass of Subplot instead of Subplotbase, but that
should be easy to implmnt.

···

from pylab import figure, show, nx
from matplotlib.figure import Figure

class MyFigure(Figure):
    def __init__(self, *args, **kwargs):
        """
        custom kwarg figtitle is a figure title
        """
        figtitle = kwargs.pop('figtitle', 'hi mom')
        Figure.__init__(self, *args, **kwargs)
        self.text(0.5, 0.95, figtitle, ha='center')

fig = figure(FigureClass=MyFigure, figtitle='my title')
ax = fig.add_subplot(111)
ax.plot([1,2,3])
fig.savefig('test.png')
fig.savefig('test.ps')
show()

_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users