为了账号安全,请及时绑定邮箱和手机立即绑定

在matplotlib中缩放图像而不更改轴

在matplotlib中缩放图像而不更改轴

慕田峪4524236 2021-03-23 17:32:44
我有一个显示图的GUI。我想将该图适合现有图像。我使用以下方法在图下显示图像:myaxe.plot(...)myaxeimage = myaxe.imshow(myimage, axpect='auto', extent=myaxe.axis(), zorder=-1)我已经可以使用myaxeimage.set_alpha()现在,我希望能够使用GUI进行放大和缩小以及在图像周围移动,而无需触及现有的绘图和轴,以便将其与我的绘图对齐。换句话说,我想按比例缩放给定的sx和sy因子,并将图像的原点放在给定的(x,y)点上,将图像的一部分裁剪到轴外。我怎样才能做到这一点?
查看完整描述

2 回答

?
神不在的星期二

TA贡献1963条经验 获得超6个赞

最后,我遵循了tcaswell的建议,并使用了2个不同的轴。这样,我只需要使用set_xlim()和调整set_ylim()图像轴,即可更改图像的原点和/或缩放比例。我想将图像隐藏在绘图下方,而不必将其与绘图框架一起隐藏,而是删除了绘图框架,并改用了图像轴框架。我还隐藏了图像轴上的刻度线。


from matplotlib import pyplot


f = pyplot.figure()

a = f.add_subplot(111, frameon=False) # Remove frame

a.plot(...)


myimg = pyplot.imread(...)

imgaxes = f.add_axes(a.get_position(), # new axes with same position

    label='image', # label to ensure imgaxes is different from a

    zorder=-1, # put image below the plot

    xticks=[], yticks=[]) # remove the ticks

img = imgaxes.imshow(myimg, aspect='auto') # ensure image takes all the place


# now, to modify things

img.set_alpha(...)

imgaxes.set_xlim((x1, x2)) # x1 and x2 must be calculated from

                           # image size, origin, and zoom factor


查看完整回答
反对 回复 2021-03-26
  • 2 回答
  • 0 关注
  • 290 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号