You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Jupyter中调整两张并排图像子图的整体尺寸?

调整Jupyter中并排子图的整体尺寸方案

因为你使用的是Matplotlib的pyplot状态机接口(而非面向对象的fig, axs写法),直接用plt.subplots的方式不会生效,你需要先创建指定尺寸的画布,再添加子图:

修改后的完整代码

import matplotlib.pyplot as plt
import matplotlib.image as img

ref_img = img.imread(ref_img_path)
test_img = img.imread(test_img_path)

# 先创建指定尺寸的画布,宽高单位为英寸,可按需调整
plt.figure(figsize=(18, 9))

plt.subplot(1, 2, 1)
plt.title("Reference Form")
plt.imshow(ref_img)
plt.subplot(1, 2, 2)
plt.title("Test Form ")
plt.imshow(test_img)

# 可选:自动调整子图间距,让布局更紧凑
plt.tight_layout()
plt.show()

关键说明

  • figsize=(18, 9)中的两个数值分别对应画布的宽度和高度,你可以根据Jupyter单元格的显示空间自由调整,比如想要更宽的画布可增大第一个数值,想要更高的画布则增大第二个数值,直到充分利用单元格空间。
  • 如果子图标题和图像有重叠,plt.tight_layout()可以自动调整子图间距、标题位置,让显示更美观。

内容的提问来源于stack exchange,提问作者dokondr

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.28 06:42:09