在matplotlib中缩放图像而不更改轴 [英] Scale image in matplotlib without changing the axis

查看:39
本文介绍了在matplotlib中缩放图像而不更改轴的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个显示绘图的 GUI.我想将该图拟合到现有图像中.我使用以下方法在图下显示图像:

I have a GUI that displays a plot. I want to fit that plot to an existing image. I displayed the image under the plot using:

myaxe.plot(...)
myaxeimage = myaxe.imshow(myimage, axpect='auto', extent=myaxe.axis(), zorder=-1)

我已经能够使用

myaxeimage.set_alpha()

现在我希望能够使用 GUI 放大和缩小图像并在图像周围移动,而无需接触现有的绘图和轴,以便将其与我的绘图对齐.换句话说,我想缩放到给定的 sx sy 因子,并将图像的原点放在给定的(x,y)点,将图像的一部分裁剪到轴外.我该怎么做?

Now I'd like to be able to zoom in and out and to move around the image, using the GUI, without touching to the existing plot and axes, in order to align it with my plot. In other words, I want to scale to given sx and sy factors, and to put origin of the image at a given (x,y) point, clipping parts of the image going outside the axes. How can I do that?

推荐答案

最后,我遵循了 tcaswell 的建议并使用了 2 个不同的轴.这样,我只需要使用图像轴的 set_xlim() set_ylim()来更改图像的原点和/或缩放系数.我为了将图像放在我的图下方,而不是用图的框架隐藏它,我删除了图的框架并使用了图像轴的框架.我还隐藏了图像轴上的刻度.

Finally, I followed tcaswell suggestion and used 2 different axes. This way, I simply have to play with set_xlim() and set_ylim() of my image axes to change the origin and/or the zooming factor of my image. I order to get the image below my plot, without hiding it with the frame of the plot, I removed the frame of the plot and used the frame of the image axes instead. I also hidden the ticks from the image axes.

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

这篇关于在matplotlib中缩放图像而不更改轴的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆