saving an axes to draw in a different figure

I'm trying to draw the axes from one figure directly over the axes for another figure, in a sense, combining the two axes as two layers on one figure.

So, first I get an axes instance, "ax".

import matplotlib.pyplot as plt
fig=plt.figure()
fig.add_subplot(111)
plt.plot(range(10), [i^2 for i in range(10)])
ax=fig.axes[0]
plt.savefig('test.png')

Okay, now I have the axes "ax". I want to draw ax directly on top of the following figure, and get a result that would be the same as if I had called the plot command above directly in the following code. All I'm passed in my real code is the newax variable below, which is why I use newax.get_figure()).

fig=plt.figure()
newax=fig.add_subplot(111)
ax.set_figure(newax.get_figure())
newax.get_figure().add_axes(ax,label="newax")
plt.savefig('test2.png')

However, the result of test2.png is not very pretty and definitely not what I want. The tick labels for the y-axis are all scrunched up, for example.

Can anyone help?

For those curious, what I'm doing is working on getting the Sage graphics code to be able to wrap and intelligently display matplotlib axes objects, so that a person could easily create a matplotlib axes, wrap it in the Sage graphics class, and then be able to manipulate it in Sage. In order for this to work, it seems like I need to save the axes object I care about, and then when Sage composes it's final figure (using matplotlib), it passes me an AxesSubplot object. I need to somehow take that subplot object and draw my saved axes on it in the most intelligent way possible. In the code above, I try taking the given AxesSubplot object, getting the figure from that, and then just adding my saved axes to that figure. Is there a better way to do this?

Thanks,

Jason

···

--
Jason Grout

I don't think your approach will work in general.
When you move an axes from one figure to the other, you have to update
the transform attributes of all the artists, which, I think, could be
tricky to do for general cases.

I'm trying to draw the axes from one figure directly over the axes for
another figure, in a sense, combining the two axes as two layers on one
figure.

So, first I get an axes instance, "ax".

import matplotlib.pyplot as plt
fig=plt.figure()
fig.add_subplot(111)
plt.plot(range(10), [i^2 for i in range(10)])
ax=fig.axes[0]
plt.savefig('test.png')

Okay, now I have the axes "ax". I want to draw ax directly on top of
the following figure, and get a result that would be the same as if I
had called the plot command above directly in the following code. All
I'm passed in my real code is the newax variable below, which is why I
use newax.get_figure()).

fig=plt.figure()
newax=fig.add_subplot(111)
ax.set_figure(newax.get_figure())
newax.get_figure().add_axes(ax,label="newax")
plt.savefig('test2.png')

For this particular case, set the figure attribute directly instead of
calling the set_figure methods, which does more extra stuff. For
example,

fig=plt.figure()
ax.figure = fig
fig.add_axes(ax,label="newax")
plt.savefig('test2.png')

At least you will have your ticks right.

Again, this approach won't work well unless you update the transform
of all the artists as well.

However, the result of test2.png is not very pretty and definitely not
what I want. The tick labels for the y-axis are all scrunched up, for
example.

Can anyone help?

For those curious, what I'm doing is working on getting the Sage
graphics code to be able to wrap and intelligently display matplotlib
axes objects, so that a person could easily create a matplotlib axes,
wrap it in the Sage graphics class, and then be able to manipulate it in
Sage. In order for this to work, it seems like I need to save the axes
object I care about, and then when Sage composes it's final figure
(using matplotlib), it passes me an AxesSubplot object. I need to
somehow take that subplot object and draw my saved axes on it in the
most intelligent way possible. In the code above, I try taking the
given AxesSubplot object, getting the figure from that, and then just
adding my saved axes to that figure. Is there a better way to do this?

Having never used Sage before, I have little idea what you want to do
here (for example, what do you mean by "Sage composes it's final
figure"?). Anyhow, I think it would be best if you can figure out some
way that does not involves axes moving around.

Regards,

-JJ

···

On Thu, Sep 17, 2009 at 1:59 PM, <jason-sage@...2130...> wrote:

Thanks,

Jason

--
Jason Grout

------------------------------------------------------------------------------
Come build with us! The BlackBerry&reg; Developer Conference in SF, CA
is the only developer event you need to attend this year. Jumpstart your
developing skills, take BlackBerry mobile applications to market and stay
ahead of the curve. Join us from November 9&#45;12, 2009. Register now&#33;
http://p.sf.net/sfu/devconf
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users

I agree that this would be difficult with the existing code base, but
it would be something that is nice to support. Perhaps we can think
about adding support for a fig.move_axes_to(otherfig) method that
reconnects all the wiring. We would of course have to be very careful
about all the child artists, but this would be a good thing to get
right.

JDH

···

On Fri, Sep 18, 2009 at 11:44 AM, Jae-Joon Lee <lee.j.joon@...287...> wrote:

I don't think your approach will work in general.
When you move an axes from one figure to the other, you have to update
the transform attributes of all the artists, which, I think, could be
tricky to do for general cases.

John Hunter wrote:

  

I don't think your approach will work in general.
When you move an axes from one figure to the other, you have to update
the transform attributes of all the artists, which, I think, could be
tricky to do for general cases.
    
I agree that this would be difficult with the existing code base, but
it would be something that is nice to support. Perhaps we can think
about adding support for a fig.move_axes_to(otherfig) method that
reconnects all the wiring. We would of course have to be very careful
about all the child artists, but this would be a good thing to get
right.
  
Okay. I was hoping that maybe you would say the right way to do this was using inset_axes or something (http://matplotlib.sourceforge.net/examples/axes_grid/inset_locator_demo.html), but I think I understand from the comments why, for example, the ticks were all off in my example.

I guess we'll put this functionality in Sage on hold pending the functionality you mention above...

Thanks,

Jason

···

On Fri, Sep 18, 2009 at 11:44 AM, Jae-Joon Lee <lee.j.joon@...287...> wrote: