展开
拓展 关闭
订阅号推广码
GitHub
视频
公告栏 关闭

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

  • 使用本地python环境可以成功执行
import pandas as pd
import matplotlib.pyplot as plt

# 设置字体
plt.rcParams['font.sans-serif'] = ['SimHei']
# 能正确显示负号
plt.rcParams['axes.unicode_minus'] = False
# 设置画布大小
plt.figure(figsize=(11, 8))

# 柱状图
path = r"C:\work\python\matplotlib_files\01.柱状图.xlsx"
data = pd.read_excel(path)
print(data)

# 排序
data.sort_values('分数', inplace=True, ascending=False)
# 绘制简单的柱形图
plt.bar(data.姓名, data.分数)
plt.show()
  • 切换为.conda\envs\dev环境后报错如下
Traceback (most recent call last):
  File "C:\work\PythonProject\demo\day23\matplotlib学习.py", line 21, in <module>
    plt.figure(figsize=(11, 8))
  File "C:\Users\ychen\.conda\envs\dev\Lib\site-packages\matplotlib\pyplot.py", line 934, in figure
    manager = new_figure_manager(
              ^^^^^^^^^^^^^^^^^^^
  File "C:\Users\ychen\.conda\envs\dev\Lib\site-packages\matplotlib\pyplot.py", line 464, in new_figure_manager
    _warn_if_gui_out_of_main_thread()
  File "C:\Users\ychen\.conda\envs\dev\Lib\site-packages\matplotlib\pyplot.py", line 441, in _warn_if_gui_out_of_main_thread
    canvas_class = cast(type[FigureCanvasBase], _get_backend_mod().FigureCanvas)
                                                ^^^^^^^^^^^^^^^^^^
  File "C:\Users\ychen\.conda\envs\dev\Lib\site-packages\matplotlib\pyplot.py", line 280, in _get_backend_mod
    switch_backend(rcParams._get("backend"))  # type: ignore[attr-defined]
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\ychen\.conda\envs\dev\Lib\site-packages\matplotlib\pyplot.py", line 343, in switch_backend
    canvas_class = module.FigureCanvas
                   ^^^^^^^^^^^^^^^^^^^
AttributeError: module 'backend_interagg' has no attribute 'FigureCanvas'. Did you mean: 'FigureCanvasAgg'?
  • 解决方案:matplotlib切换图形界面显示终端TkAgg
import pandas as pd

import matplotlib
matplotlib.use('TkAgg')
import matplotlib.pyplot as plt
posted @ 2023-12-03 11:52  DogLeftover  阅读(3764)  评论(0编辑  收藏  举报