【python_pytorch_ matplotlib】matplotlib不兼容:在jupyter中正常使用,转到pycharm中会报错无法运行

problem

matplotlib : incompatible version

bash 复制代码
....
bash 复制代码
Traceback (most recent call last):
File "D:\\Anaconda\envs\pytorch\lib\site-packages\IPython\core\interactiveshell.py", line 3579, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "<ipython-input-2-cf950f1e0bb2>", line 10, in <module>
plt.figure()
File "D:\\Anaconda\envs\pytorch\lib\site-packages\matplotlib\pyplot.py", line 1042, in figure
manager = new_figure_manager(
File "D:\\Anaconda\envs\pytorch\lib\site-packages\matplotlib\pyplot.py", line 551, in new_figure_manager
_warn_if_gui_out_of_main_thread()
File "D:\\Anaconda\envs\pytorch\lib\site-packages\matplotlib\pyplot.py", line 528, in _warn_if_gui_out_of_main_thread
canvas_class = cast(type[FigureCanvasBase], _get_backend_mod().FigureCanvas)
File "D:\\Anaconda\envs\pytorch\lib\site-packages\matplotlib\pyplot.py", line 369, in _get_backend_mod
switch_backend(rcParams._get("backend"))
File "D:\\Anaconda\envs\pytorch\lib\site-packages\matplotlib\pyplot.py", line 426, in switch_backend
canvas_class = module.FigureCanvas
AttributeError: module 'backend_interagg' has no attribute 'FigureCanvas'. Did you mean: 'FigureCanvasAgg'?

core problem

AttributeError : module 'backend_interagg' has no attribute 'FigureCanvas'. Did you mean: 'FigureCanvasAgg'?

version?


但这个环境在jupyter中是可以正常使用的?
matpoltlib在jupyter中正常使用但在pycharm中运行报错

problem process

  1. 对matplotlib版本进行升级:
    打开你的 Anaconda Prompt (或者在你的IDE的终端里,确保已经激活了 pytorch 环境),然后执行以下命令:

·pip install --upgrade -- force-reinstall matplotlib·

-- upgrade: 确保升级到最新版本。

-- force-reinstall: 强制重新安装,即使当前已是最新版本,它也会重新下载并覆盖现有文件,能有效修复文件损坏问题。

额外指令:conda install --force-reinstall matplotlib'''

  1. add end 添加后端
    如果重新安装后问题依旧,你可以在代码的开头手动指定一个稳定且常用的后端,从而绕过有问题的默认后端。
    import matplotlib matplotlib.use('Agg') # 或者 'TkAgg'

matplotlib.use('Agg'):这行代码必须在 import matplotlib.pyplot as plt 之前执行。

Agg 是一个高质量的非交互式后端,它会将图像渲染到文件中(比如 plt.savefig() 会正常工作),但不会在屏幕上显示交互式窗口 (plt.show() 可能不会弹出窗口,具体行为取决于你的 IDE)。

TkAgg 是一个常用的交互式后端,它会弹出一个窗口来显示你的图像。如果 Agg 能解决 savefig 的问题但你还想用 plt.show() 弹出窗口,可以试试 matplotlib.use('TkAgg')。

建议使用方法一(强制重新安装),因为添加后端不能从根本上解决问题

相关推荐
一 乐6 小时前
婚纱摄影网站|基于ssm + vue婚纱摄影网站系统(源码+数据库+文档)
前端·javascript·数据库·vue.js·spring boot·后端
期待のcode8 小时前
前后端分离项目 Springboot+vue 在云服务器上的部署
服务器·vue.js·spring boot
ProgramHan9 小时前
Spring Boot 3.2 新特性:虚拟线程的落地实践
java·jvm·spring boot
源码获取_wx:Fegn089510 小时前
基于 vue智慧养老院系统
开发语言·前端·javascript·vue.js·spring boot·后端·课程设计
毕设源码_郑学姐10 小时前
计算机毕业设计springboot基于HTML5的酒店预订管理系统 基于Spring Boot框架的HTML5酒店预订管理平台设计与实现 HTML5与Spring Boot技术驱动的酒店预订管理系统开
spring boot·后端·课程设计
不吃香菜学java11 小时前
spring-依赖注入
java·spring boot·后端·spring·ssm
南部余额11 小时前
Spring Boot 整合 MinIO:封装常用工具类简化文件上传、启动项目初始化桶
java·spring boot·后端·文件上传·工具类·minio·minioutils
QQ196328847511 小时前
ssm基于Springboot+的球鞋销售商城网站vue
vue.js·spring boot·后端
太空眼睛11 小时前
【MCP】使用SpringBoot基于Streamable-HTTP构建MCP-Server
spring boot·sse·curl·mcp·mcp-server·spring-ai·streamable
幽络源小助理11 小时前
springboot校园车辆管理系统源码 – SpringBoot+Vue项目免费下载 | 幽络源
vue.js·spring boot·后端