Hi, I have a function fig(x) that returns a subplot
> instance, and I'd like to make a new figure by calling this
> function twice.
> For example: def fig(x): s = subplot(111) return s.plot(x)
> and i wan't to do something like:
> fig = figure(figsize = (6,12)) fig.add_axes(fig(x1))
> ax1, ax2 = fig.get_axes() ax1.set_position([.125, .5, .75,
> But it looks like the position is not understood relative
> to the new figure size, so it doesn't work. Should it or
> is there a better way to do that ?
There are lots of problems with your code that makes it hard to
understand what you are trying to do. Let's get some terminology
straight by way of commenting your code, then maybe you can describe
what you really want to do
# subplot creates an Axes instance on a regular grid; it will add
# this Axes to the current Figure, and if no Figure exists, it will
# create on. If a current Figure exists, and you have already
# created an Axes in it with subplot(111), this call simply makes
# the Axes the current Axes. Technically, it creates a Subplot,
# which is derived from Axes
s = subplot(111)
# plot returns a list of Line2D objects, and so the function "fig"
# is returning a list of lines
# this call creates a Figure instance
fig = figure(figsize = (6,12))
# add_axes is used to add an Axes to the figure. It expects either an
# Axes instance, or a rectangle determined by [left, bottom, width,
# height]. You are passing it a list of lines, as noted above
# this returns a list of axes
ax1, ax2 = fig.get_axes()
# this sets the rectangle for the first Axes
ax1.set_position([.125, .5, .75, .4])
OK, now that we have some terminology down and hopefully you can see
why you are failing, I'll try and guess what you want. You want to
add an Axes to the current figure, and have them stacked one above the
other. So on the first call, you basically have subplot(111), and on
the second call, you have two subplots, subplot(211) and subplot(212).
from pylab import figure, show
def pushax(fig, x):
n = len(fig.axes)
for i, ax in enumerate(fig.axes):
print 'new gemo', n+1,1,i+1
ax = fig.add_subplot(n+1, 1, n+1)
fig = figure()
See also the devel list, where Andrew Straw recently added a "Sizer"
model along the lines of wx sizers for axes placement.
Or I may have misunderstood what you want to do....