11

I think this is quite easy but I searched the internet and matplotlib users mailing list and not able to find an answer. ax2 is an inset axes within the "ax" axes in figure "fig", which I make by following here: http://matplotlib.sourceforge.net/examples/pylab_examples/axes_demo.html

but now my problem is that I cannot fix the ax2 the exact position I want, it seems that draw() command change this:

In [352]: ax2.set_position([0.125,0.63,0.25,0.25]) In [353]: ax2.get_position() Out[353]: Bbox(array([[ 0.125, 0.63 ], [ 0.375, 0.88 ]])) In [354]: draw() In [355]: ax2.get_position() Out[355]: Bbox(array([[ 0.15625, 0.63 ], [ 0.34375, 0.88 ]])) 

notice that, after "draw()" command, the x0 of ax2 changed. could anyone give any hints?

thanks!

6
  • What data is being plotted? I'm particularly interested in the x axis scaling. Commented Jun 17, 2012 at 18:02
  • what do you mean by a axis scaling? Commented Jun 18, 2012 at 19:58
  • I did a test on my side, and got different results: import matplotlib.pyplot as plt fig = plt.figure(figsize=plt.figaspect(0.75)) ax2 = fig.add_subplot(111) ax2.set_position([0.125,0.63,0.25,0.25]) ax2.get_position() Bbox(array([[ 0.125, 0.63 ],[ 0.375, 0.88 ]])) plt.draw() ax2.get_position() Bbox(array([[ 0.125, 0.63 ],[ 0.375, 0.88 ]])) Commented Jun 19, 2012 at 17:15
  • 2
    yes. I forgot to add following comments. Actually in my script there is one step to set equal aspect. I guess when I use draw, it applies this command and the default anchor for set_aspect ('equal') is 'center' I guess. That's why the position changed. Commented Jun 20, 2012 at 20:36
  • 1
    wiswit: It might be a good idea to answer your own question. This helps when people have the same question as you, they can find the answer easily. Commented Jun 26, 2012 at 14:23

1 Answer 1

6

Try setting the position by specifying the parameters as a Bbox object:

>>> ax2.set_position(matplotlib.transforms.Bbox(array([[0.125,0.63],[0.25,0.25]]))) >>> ax2.get_position() Bbox(array([[ 0.125, 0.63 ], [ 0.25 , 0.25 ]])) >>> draw() >>> ax2.get_position() Bbox(array([[ 0.125, 0.63 ], [ 0.25 , 0.25 ]])) 

With this you can see the settings are round-tripping like you would expect.

In addition, you could instead look at the results as a tuple like so:

>>> a.set_position([0.125,0.63,0.25,0.25]) >>> a.get_position().bounds (0.125, 0.63, 0.25, 0.25) 

It just depends if you want to look at the location/position as a bounding box: x0,y0 by x1,y1, or instead by a location and size: x,y,width,height.

Currently you are telling it to set a x,y,width,height, then it's telling you the x0,y0,x1,y1.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.