如何使用imgshow使一个子地块图像变大,以对齐两个图像?

2024-04-23 06:28:19 发布

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

我有很多子图片,想把左边的图片放大一点,这样轴就对齐了。我该怎么做

from matplotlib import pyplot as plt
import matplotlib.image as mpimg

img1 = mpimg.imread('../Reproducibility_Paper_Figures/noord_graph_image.png')
img2 = mpimg.imread('../Reproducibility_Paper_Figures/full_comparison.png')


f = plt.figure(figsize=(28.0,20.0))
f.add_subplot(1,2,1)
plt.imshow(img2,interpolation='none')
plt.axis('off')
f.add_subplot(1,2, 2)
plt.imshow(img1,interpolation='none')
plt.axis('off')
#plt.show(block=True)

f.tight_layout(pad=-5)
plt.savefig('../Reproducibility_Paper_Figures/side.png')

这些图像看起来像:

the images


Tags: imageimportaddpngmatplotlibas图片plt
1条回答
网友
1楼 · 发布于 2024-04-23 06:28:19

获得所需对齐的一种方法是显式设置图像的范围。范围将是数据的轴。然后使用sharey=True将两者对齐。如果轴提供有用的信息,请使其可见,否则将其禁用。在演示代码中,将显示所有记号,以帮助找到范围的正确值

from matplotlib import pyplot as plt
import matplotlib.image as mpimg

img1 = mpimg.imread('image1.png')
img2 = mpimg.imread('image2.png')

fig, (ax1, ax2) = plt.subplots(figsize=(12.0, 5.0), ncols=2, sharey=True)

ax1.imshow(img2, interpolation='bilinear', extent=[0, 120, -21, 360], aspect=1/4)
# ax1.axis('off')
ax1.tick_params(labeltop=True, labelbottom=True, labelright=True, labelleft=True,
                top=True, bottom=True, right=True, left=True)
ax1.yaxis.set_major_locator(plt.MultipleLocator(20))
ax2.imshow(img1, interpolation='bilinear', extent=[0, 120, -37.5, 361], aspect=1/4)
ax2.axis('on')
ax2.tick_params(labeltop=True, labelbottom=True, labelright=True, labelleft=True,
                top=True, bottom=True, right=True, left=True)
# ax2.axis('off')
# plt.savefig('../Reproducibility_Paper_Figures/side.png')
plt.show()

resulting images

PS:如果要同时使用savefigshow,请确保先调用savefigplt.show将擦除图像

相关问题 更多 >