在不更改轴的情况下缩放matplotlib中的图像

2024-06-01 01:39:58 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个图形用户界面,显示一个情节。我想把这张图和现有的图像吻合。我在绘图下面显示了图像,使用:

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

我已经可以使用

myaxeimage.set_alpha()

现在,我想能够放大和缩小,并在图像周围移动,使用图形用户界面,而不接触现有的绘图和轴,以使它与我的绘图。换言之,我希望缩放到给定的sxsy因子,并将图像的原点放在给定的(x,y)点上,剪切图像超出轴的部分。我该怎么做?


Tags: 图像绘图autoplot图形用户界面extentsetimshow
2条回答

有一个watermark example与matplotlib一起分布,有点类似。从该代码开始,我们可以修改如下:

使用ax.imshow先绘制图像。我这样做是因为extent参数影响ax的最终范围。因为我们希望最后的范围由plt.plot(...)控制,所以让我们把它放在最后。

myaximage = ax.imshow(im, aspect='auto', extent=(1,15,0.3,0.7), alpha=0.5, origin='upper', zorder=-1)

使用extent来控制图像的位置和大小,而不是extent=myaxe.axis()extent=(1,15,0.3,0.7)将图像放置在矩形中,(1, 0.3)作为左下角,(15, 0.7)作为右上角。

使用origin='upper',数组im[0,0]索引放置在区段的左上角。使用origin='lower'时,它应该放在左下角。


import numpy as np
import matplotlib.pyplot as plt
import matplotlib.cbook as cbook
import matplotlib.image as image
np.random.seed(1)
datafile = cbook.get_sample_data('logo2.png', asfileobj=False)
im = image.imread(datafile)
fig, ax= plt.subplots()

myaximage = ax.imshow(im, aspect='auto', extent=(1,15,0.3,0.7), alpha=0.5, zorder=-1)
ax.plot(np.random.rand(20), '-o', ms=20, lw=2, alpha=1.0, mfc='orange')
ax.grid()
plt.show()

enter image description here


如果要展开图像并将其剪辑到绘图范围,可能还需要使用ax.set_xlimax.set_ylim

myaximage = ax.imshow(im, aspect='auto', extent=(-1,25,0.3,0.7), alpha=0.5, zorder=-1,
                      origin='upper')

ax.plot(np.random.rand(20), '-o', ms=20, lw=2, alpha=1.0, mfc='orange')
ax.set_xlim(0,20)
ax.set_ylim(0,1)

enter image description here


或者,为了获得更多控制,可以使用myaximage.set_clip_path将图像剪辑到任意路径:

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.cbook as cbook
import matplotlib.image as image
import matplotlib.patches as patches
np.random.seed(1)
datafile = cbook.get_sample_data('logo2.png', asfileobj=False)
im = image.imread(datafile)
fig, ax= plt.subplots()

myaximage = ax.imshow(im, aspect='auto', extent=(-5,25,0.3,0.7), 
                      alpha=0.5, origin='upper',
                      zorder=-2)
# patch = patches.Circle((300,300), radius=100)
patch = patches.Polygon([[5, 0.4], [15, 0.4], [15, 0.6], [5, 0.6]], closed=True,
                        transform=ax.transData)
myaximage.set_clip_path(patch)
ax.plot(np.random.rand(20), '-o', ms=20, lw=2, alpha=1.0, mfc='orange', 
        zorder=-1)

ax.set_xlim(0, 20)
ax.set_ylim(0, 1)

plt.show()

enter image description here

最后,我遵循tcaswell的建议,使用了两个不同的轴。这样,我只需使用图像轴的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

相关问题 更多 >