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

使用Subfigure与GridSpec时Matshow子图逐渐缩小的问题求助

Subfigure与GridSpec时Matshow子图逐渐缩小的问题求助

大家好,我最近在用Matplotlib做可视化时碰到了一个头疼的问题:当我在subfigure里结合gridspec布局添加matshow子图时,这些matshow图像会变得越来越小,后面的子图里的图像几乎快看不清了。

以下是我完整的测试代码:

import numpy as np
import matplotlib.pyplot as plt

mask = np.array([
    [ 1, 0, 1, 0, 1],
    [ 1, 1, 1, 1, 1],
    [ 0, 0, 0, 0, 0],
    [ 1, 1, 1, 1, 1],
    [ 1, 1, 1, 1, 0]
])

fig = plt.figure(figsize = (12,16), layout = "constrained")
figures = fig.subfigures(4, 2)
figures = figures.reshape(-1)

x = np.linspace(0, 1, 100)
y = x**(0.5)

for f in figures:
    spec = f.add_gridspec(3, 2)
    for i in range(3):
        image_axis = f.add_subplot(spec[i, 0])
        image_axis.matshow(mask, cmap = 'grey')
    plot_axis = f.add_subplot(spec[:,1])
    plot_axis.plot(x, y)

plt.savefig('test.png')

运行这段代码后,生成的图片里,左侧的matshow子图从第一个到最后一个尺寸逐渐缩小,而右侧的折线图始终保持正常大小。我反复检查了布局代码,但没发现哪里有问题,有没有熟悉Matplotlib布局的朋友能帮我分析一下原因,或者给出解决办法呀?

内容来源于stack exchange

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.08 11:19:33