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

Matplotlib网格中隐藏坐标异常:仅右下角生效,求解决方法

问题:Matplotlib网格布局中仅右下角坐标轴被隐藏,其余轴仍显示

尝试在Matplotlib的网格布局中隐藏所有坐标,但无论怎么操作,只有右下角的坐标/轴被隐藏,其余所有轴仍显示,已无其他尝试思路。

原代码如下:

import matplotlib.pyplot as plt
from PIL import Image

fig,ax = plt.subplots(6,7)
plt.axis('off')

filenames=['gelb.png'.format(i) for i in range(42)]
for i in range(42):
    #plt.axis('off')
    with open(filenames[i],'rb') as f:
        image=Image.open(f)
        ax[i%6][i//6].imshow(image)
fig.show()
解决方案

plt.axis('off')仅对当前活跃的坐标轴生效(也就是最后创建的右下角子图轴),无法批量作用于所有子图。要隐藏全部子图的坐标轴,需要遍历每一个子图的轴对象,单独调用关闭方法。

修改后的代码:

import matplotlib.pyplot as plt
from PIL import Image

fig, ax = plt.subplots(6,7)

# 遍历所有子图轴,逐个关闭坐标轴
for row in ax:
    for axis in row:
        axis.axis('off')

# 修正文件名生成:如果需要不同文件名,改为'gelb{}.png'.format(i)
filenames=['gelb.png' for i in range(42)]
for i in range(42):
    with open(filenames[i],'rb') as f:
        image = Image.open(f)
        ax[i%6][i//6].imshow(image)
fig.show()

关键说明

  • 双层循环遍历ax的每一行和每个轴对象,确保所有子图都执行axis('off'),实现批量隐藏坐标轴。
  • 原代码中'gelb.png'.format(i)无实际替换效果(字符串无占位符),若需生成带序号的文件名,调整为'gelb{}.png'.format(i)即可。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.15 20:15:33