python列表方法position,如何在python matplotlib中使用set_position方法固定轴位置?

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!

解决方案

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值