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

绘图正常显示但报错:module 'matplotlib' has no attribute 'pyplot'

解决matplotlib绘图成功但触发AttributeError的问题

问题现象

使用matplotlib.pyplot执行简单绘图,图像能正常显示,但调用plt.show()时触发AttributeError,报错指向matplotlib.pyplot.close('all')找不到属性,多次卸载重装matplotlib、conda环境安装均无效。

可行解决方法

  • 修复matplotlib_inline版本兼容问题
    报错发生在matplotlib_inline后端调用plt.close()时,大概率是版本不匹配。执行以下命令升级或降级:

    # 升级到最新版
    pip install --upgrade matplotlib_inline
    # 或降级到稳定兼容版
    pip install matplotlib_inline==0.1.6
    
  • 切换matplotlib后端
    临时禁用inline后端,改用GUI后端,在代码开头添加:

    import matplotlib
    matplotlib.use('TkAgg')  # Windows系统可选'Qt5Agg',macOS可选'QtAgg'
    import matplotlib.pyplot as plt
    
    plt.plot([1,2])
    plt.show()
    
  • 重建干净的conda环境
    环境依赖冲突可能导致异常,彻底重建环境:

    # 删除现有music环境
    conda remove -n music --all
    # 创建新环境(指定合适Python版本,比如3.9)
    conda create -n music python=3.9
    # 激活环境并安装matplotlib
    conda activate music
    conda install matplotlib
    
  • 排查本地文件干扰
    确保当前工作目录没有名为matplotlib.py或matplotlib_inline.py的脚本,避免覆盖库文件。可运行以下代码验证导入路径:

    import matplotlib
    import matplotlib_inline
    print("matplotlib路径:", matplotlib.__file__)
    print("matplotlib_inline路径:", matplotlib_inline.__file__)
    

    输出应指向conda环境下的site-packages目录,而非本地脚本。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.29 09:07:46