【AI 问题集】AttributeError: module ‘backend_interagg‘ has no attribute ‘FigureCanvas‘

目录


1、问题展示

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'?

2.问题原因

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

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

源码

    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

3.问题解决方案

修改backend 方案,由于是通过 matplotlib引入 ,因此可以将matplotlib.backends进行修改

# make sure the init is pulled up so we can assign to it later
import matplotlib.backends

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"]

我们修改 backend 配置:matplotlib.use(‘TkAgg’)

4、修改代码如下

4.1 修改前


import matplotlib.pyplot as plt



def do_test():
    plt.plot([1, 2, 3, 4])
    plt.ylabel('some numbers')
    plt.show()


if __name__ == '__main__':
    do_test()

4.2修改后

import matplotlib
import matplotlib.pyplot as plt


matplotlib.use('TkAgg')



def do_test():
    plt.plot([1, 2, 3, 4])
    plt.ylabel('some numbers')
    plt.show()


if __name__ == '__main__':
    do_test()

5、代码执行结果

执行官网例子,将列表画成线

版权声明:本文为博主作者:lxtx-0510原创文章,版权归属原作者,如果侵权,请联系我们删除!

原文链接:https://blog.csdn.net/weixin_38996079/article/details/135141925

共计人评分,平均

到目前为止还没有投票!成为第一位评论此文章。

(0)
乘风的头像乘风管理团队
上一篇 2024年4月10日
下一篇 2024年4月10日

相关推荐