【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')。

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

相关推荐
终将老去的穷苦程序员1 天前
基于SpringBoot的餐饮管理系统
java·spring boot·后端
心之伊始1 天前
Spring AI Tool Calling 实战:让 Java Agent 调用本地 Bean 工具方法
java·spring boot·agent·spring ai·tool calling
熠熠仔1 天前
Spring Boot 与 MyBatis-Plus 空间几何数据集成指南
spring boot·后端·mybatis
天青色等烟雨..1 天前
智慧农林核心遥感技术99个案例实践
运维·人工智能·spring boot·后端·自动化
雪宫街道2 天前
SpringBoot 向 IOC 容器注册组件的两种姿势:@Configuration 与 @Import
java·spring boot·后端·spring
接着奏乐接着舞2 天前
springboot mp mybatis plaus
windows·spring boot·mybatis
金融支付架构实战指南2 天前
Milvus 向量检索服务 + SpringBoot 实战:电商商品语义检索与相似商品推荐
spring boot·后端·milvus·向量检索
愛~杦辷个訾2 天前
Java Springboot使用阿里云oss对图片进行等质量压缩,转换成webp格式的压缩图。
java·spring boot·阿里云·oss
霸道流氓气质2 天前
Spring Boot Multipart 表单中文乱码问题全解析
java·spring boot·后端
RuoyiOffice2 天前
从 0 到 1 搭建 RuoyiOffice:30 分钟跑通后端+前端+移动端
前端·spring boot·uni-app·开源·oa·ruoyioffice·hrm