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

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

相关推荐
阿拉斯攀登21 小时前
安卓工控机 OTA 升级方案(SpringBoot+MQTT)
android·spring boot·物联网·iot
JIngJaneIL1 天前
智慧物业|物业管理|基于SprinBoot+vue的智慧物业管理系统(源码+数据库+文档)
java·前端·数据库·vue.js·spring boot·论文·智慧物业管理系统
A3608_(韦煜粮)1 天前
深入理解 Spring Boot 自动配置:原理、定制与最佳实践摘要
spring boot·自动配置·自定义starter·源码解析·条件注解·spring框架·java配置
小马爱打代码1 天前
SpringBoot + Quartz + Redis:分布式任务调度系统 - 从架构设计到企业级落地
spring boot·redis·分布式
q***87601 天前
Spring Boot 整合 Keycloak
java·spring boot·后端
Billow_lamb1 天前
Spring Boot2.x.x全局拦截器
java·spring boot·后端
泉城老铁1 天前
Springboot对接mqtt
java·spring boot·后端
q***98521 天前
VS Code 中如何运行Java SpringBoot的项目
java·开发语言·spring boot
allbs1 天前
spring boot项目excel导出功能封装——3.图表导出
spring boot·后端·excel
wasp5201 天前
Spring AI 代码分析(十)--Spring Boot集成
人工智能·spring boot·spring