Subclassing Figure and pylab.figure()

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

Well, we could keep it simple and just give a hook to custom figures.
If a user wants a custom subplot

class MyFigure(Figure):
   def add_my_subplot(self, *args, **kwargs):
        self.axes.append(MySubplot(*args, **kwargs))

fig = figure(FigureClass=MyFigure)
fig.add_my_subplot(111)

Is there any downside to this approach? It seems like if you are
going to the trouble to create a custom figure, you might was well
handle your own custom subplots there rather than in the Figure class
proper.

JDH

John

Well, we could keep it simple and just give a hook to custom figures.
If a user wants a custom subplot

class MyFigure(Figure):
   def add_my_subplot(self, *args, **kwargs):
        self.axes.append(MySubplot(*args, **kwargs))

Is there any downside to this approach? It seems like if you are
going to the trouble to create a custom figure, you might was well
handle your own custom subplots there rather than in the Figure class
proper.

Try that:
fig = figure(FigureClass=MyFigure)
fsplist = [Subplot(fig,211), Subplot(fig,212), Subplot(fig,211)]
for fsp in fsplist:
  fig.add_my_subplot(fsp)

You should end up with three subplots, with fsp[-1] and fsp[0] at the same
location 211, when in fact you really need only two subplots (assume that the
last one is an updated version of the first one, which is not obvious in this
example....). In short, the `add_my_subplot` does not reproduce the behaviour
of `add_subplot`.

FYI, when I started working on that yesterday evening, I wrote something quite
similar to what you're suggesting. Then, I needed to go through all my
subplots, and instead of checking the `figure.axes` list, I checked
`fig._axstack_elements`. Which was empty, of course. So I added a
`self._axstack.push(MySubplot`) to `add_my_subplot`, then noted that I had to
put back the tests on whether the dubplot was already present or not
(figure._seen), and before soon, I was just copying the basic 'add_subplot()'
around that `self.axes.append(MySubplot)`. As I have several custom figures
with custom subplots, I realized it'd be cleaner to just make a
`add_generic_subplot` instead of copying/pasting most of the code.

I'm realizing now that had I worked on `figure.axes` straight away, I would
never have had the proper behaviour, so I've been lucky.