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

调用subplot_mosaic报错:'Figure'对象无该属性,疑版本问题

解决'Figure' object has no attribute 'subplot_mosaic'报错问题

核心原因

subplot_mosaic是Matplotlib 3.3.0版本才新增的API,你的Matplotlib版本低于3.3.0时,Figure对象就不会有这个方法,直接调用就会触发报错。

解决步骤

  • 检查当前Matplotlib版本
    运行以下代码确认版本:

    import matplotlib
    print(matplotlib.__version__)
    

    如果输出小于3.3.0,就需要升级。

  • 升级Matplotlib
    根据你的环境选择对应的升级命令:

    • 普通pip环境:
      pip install --upgrade matplotlib
      
    • Conda环境(Spyder常搭配Anaconda使用):
      conda update matplotlib
      
  • 验证修复效果
    重新运行版本检查代码,确认版本≥3.3.0后,再执行你的原代码即可正常生成子图布局。

临时替代方案(无法升级时)

如果暂时无法升级Matplotlib,可以用你已导入的gridspec手动实现相同布局,示例代码如下:

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec

fig = plt.figure(figsize=(5,5))
gs = gridspec.GridSpec(2, 2)

# 创建对应子图
ax_logo = fig.add_subplot(gs[0, 0])
ax_text = fig.add_subplot(gs[0, 1])
ax_bar = fig.add_subplot(gs[1, :])  # 跨两列

# 添加标注
ax_logo.annotate(xy=(.5,.5), text="logo", ha="center", va="center", size=20)
ax_text.annotate(xy=(.5,.5), text="text", ha="center", va="center", size=20)
ax_bar.annotate(xy=(.5,.5), text="bar", ha="center", va="center", size=20)

plt.show()

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.14 20:01:11