AttributeError: module ‘backend_interagg‘ has no attribute ‘FigureCanvas’问题解决

python 复制代码
Traceback (most recent call last):
  File "E:\workspace\python_project\aitest\test1.py", line 20, in <module>
    do_test()
  File "E:\workspace\python_project\aitest\test1.py", line 15, in do_test
    plt.plot(x, y, 'r-', linewidth=3)
  File "E:\workspace\python_project\aitest\venv\lib\site-packages\matplotlib\pyplot.py", line 3575, in plot
    return gca().plot(
  File "E:\workspace\python_project\aitest\venv\lib\site-packages\matplotlib\pyplot.py", line 2525, in gca
    return gcf().gca()
  File "E:\workspace\python_project\aitest\venv\lib\site-packages\matplotlib\pyplot.py", line 1000, in gcf
    return figure()
  File "E:\workspace\python_project\aitest\venv\lib\site-packages\matplotlib\pyplot.py", line 934, in figure
    manager = new_figure_manager(
  File "E:\workspace\python_project\aitest\venv\lib\site-packages\matplotlib\pyplot.py", line 464, in new_figure_manager
    _warn_if_gui_out_of_main_thread()
  File "E:\workspace\python_project\aitest\venv\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 "E:\workspace\python_project\aitest\venv\lib\site-packages\matplotlib\pyplot.py", line 280, in _get_backend_mod
    switch_backend(rcParams._get("backend"))  # type: ignore[attr-defined]
  File "E:\workspace\python_project\aitest\venv\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'?

问题原因

从问题中可以看到 :module 是设置成 backend_interagg 即 backend 是 agg

从源码中,我们可以看到,matplotlib.pylot 的默认 backend 是设置成agg

python 复制代码
    global _backend_mod
    # make sure the init is pulled up so we can assign to it later
    import matplotlib.backends
 
    if newbackend is rcsetup._auto_backend_sentinel:
        current_framework = cbook._get_running_interactive_framework()
        mapping = {'qt': 'qtagg',
                   'gtk3': 'gtk3agg',
                   'gtk4': 'gtk4agg',
                   'wx': 'wxagg',
                   'tk': 'tkagg',
                   'macosx': 'macosx',
                   'headless': 'agg'}
 
        if current_framework in mapping:
            candidates = [mapping[current_framework]]
        else:
            candidates = []
        candidates += [
            "macosx", "qtagg", "gtk4agg", "gtk3agg", "tkagg", "wxagg"]
 
        # Don't try to fallback on the cairo-based backends as they each have
        # an additional dependency (pycairo) over the agg-based backend, and
        # are of worse quality.
        for candidate in candidates:
            try:
                switch_backend(candidate)
            except ImportError:
                continue
            else:
                rcParamsOrig['backend'] = candidate
                return
        else:
            # Switching to Agg should always succeed; if it doesn't, let the
            # exception propagate out.
            switch_backend("agg")
            rcParamsOrig["backend"] = "agg"
            return

解决方案

1、安装matplotlib3.5.0及以下版本
python 复制代码
pip install matplotlib==3.5.0
2、指定后端TkAgg
复制代码
import matplotlib
matplotlib.use('TkAgg')

拓展

一次性显示多个图
复制代码
plt.figure()
#figure1代码

plt.figure()
#figure2代码
#......

plt.show()
#只需要每张图前加入figure(),最后加show()即可
多条曲线绘制在一张图
复制代码
plt.figure()
plt.plot(x1,y1)
plt.plot(x2,y2)
#......
plt.show()
#一个figure()+一个show()
一次只弹出一个独立窗口
复制代码
#figure1
plt.figure()
plt.show()
#figure2
plt.figure()
plt.show()
#程序会一直等待手动关闭一个窗口后再继续运行代码
相关推荐
一晌小贪欢37 分钟前
【Python数据分析】数据分析与可视化
开发语言·python·数据分析·数据可视化·数据清洗
草莓火锅2 小时前
用c++使输入的数字各个位上数字反转得到一个新数
开发语言·c++·算法
j_xxx404_2 小时前
C++ STL:阅读list源码|list类模拟|优化构造|优化const迭代器|优化迭代器模板|附源码
开发语言·c++
DreamNotOver2 小时前
批量转换论文正文引用为上标
开发语言·论文上标
散峰而望2 小时前
C/C++输入输出初级(一) (算法竞赛)
c语言·开发语言·c++·算法·github
fie88893 小时前
基于MATLAB的狼群算法实现
开发语言·算法·matlab
gihigo19983 小时前
MATLAB中生成混淆矩阵
开发语言·matlab·矩阵
dreams_dream3 小时前
Flask
后端·python·flask
曾几何时`3 小时前
C++——this指针
开发语言·c++
小冯的编程学习之路3 小时前
【C++】: C++基于微服务的即时通讯系统(1)
开发语言·c++·微服务