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

这篇具有很好参考价值的文章主要介绍了【AI 问题集】AttributeError: module ‘backend_interagg‘ has no attribute ‘FigureCanvas‘。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

目录

1、问题展示

2.问题原因

3.问题解决方案

4、修改代码如下

4.1 修改前

4.2修改后

5、代码执行结果


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的

attributeerror: module 'backend_interagg' has no attribute 'figurecanvas,人工智能,人工智能,python,matplotlib

源码

    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、代码执行结果

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

attributeerror: module 'backend_interagg' has no attribute 'figurecanvas,人工智能,人工智能,python,matplotlib文章来源地址https://www.toymoban.com/news/detail-849495.html

到了这里,关于【AI 问题集】AttributeError: module ‘backend_interagg‘ has no attribute ‘FigureCanvas‘的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 解决 AttributeError: module ‘tensorflow‘ has no attribute ‘contrib‘ 等类似的问题

    使用tensorflow2.x版本的时候,使用调用tensorflow1.x函数的代码时,常常会出现module ‘tensorflow’ has no attribute ‘contrib’这样的问题,这是由于tensorflow2.x废弃了很多tensorflow1.x API接口,本文针对常见的几种错误来使tf2.0不降版本也能运行代码。 1、报错AttributeError: module ‘tensorflow’

    2024年02月02日
    浏览(33)
  • AttributeError: module ‘torch.nn‘ has no attribute ‘SiLU‘问题的解决办法

    AttributeError: module \\\'torch.nn\\\' has no attribute \\\'SiLU\\\'        这个问题的原因是这个版本的torch中的torch.nn函数表里面没有这个SiLU函数,需要更高版本的torch,在官方的文档中我们就可以查看这个版本里有没有包含这个模块。 官方文档1.1.0    红框选择 torch 版本,我的版本是1.1.0,搜

    2024年02月13日
    浏览(41)
  • 解决 AttributeError: module ‘numpy‘ has no attribute ‘int‘ 训练yolo时的问题

    在运行yolov5的train.py出现这个报错结果。 看其他博主说的是因为库版本不对应,需要更换python或numpy版本; 原因:numpy.int在NumPy 1.20中已弃用,在NumPy 1.24中已删除。 解决方式:将numpy.int更改为numpy.int_,int。 本人一开始试了上述方法,报出了别的错,继续修改,又返回了这个

    2024年02月11日
    浏览(30)
  • 解决paddle.fluid与python版本不匹配的问题:AttributeError: module ‘paddle.fluid‘ has no attribute ‘xxx‘

    今天跑paddle里的代码,发现paddle里可以跑,但放到pycharm下面跑不了了,捣鼓了一下午,原来是包版本的问题,希望能对读者有一点点用。 我原本所用的环境是python3.11,它的paddlepaddle包的版本是2.5,然后我去paddle官网上看我要用的代码的配置环境(我在官网上可以跑通),打

    2024年02月03日
    浏览(32)
  • [记录解决YOLOv5加载权重文件报错问题]AttributeError: Can‘t get attribute ‘DetectionModel‘ on <module ‘models.yolo‘

    1.GitHub(链接: link)找到更新后的v5版本。 2.找到models文件夹下的yolo.py,查找DetectionModel模块,将图片中的代码(Segment部分、BaseModel部分、DetectionModel部分)粘贴至报错的yolo.py里。 3.粘贴完后,运行yolo.py文件

    2024年02月12日
    浏览(31)
  • warning: setUpNet DNN module was not built with CUDA backend; switching to CPU错误解决

    利用opencv-python的dnn模块调用yolo3模型进行目标检测的时候,根据网上的教程,加入GPU加速,也就是加入如下的两行代码: 但是在运行之后,出现了这样的提示: 这说明GPU没被用上,原因是OPENCV在安装的时候,没有安装支持CUDA的版本,所以需要重新编译安装OPENCV。 安装OPENC

    2024年02月17日
    浏览(46)
  • AttributeError: module ‘torch.nn‘ has no attribute ‘module‘

    如上,调用时报错: init () takes 1 positional argument but 2 were given 纠错发现是少打了下划线 init前后有两个_,

    2024年02月16日
    浏览(29)
  • 解决AttributeError: module ‘keras‘ has no attribute ……

    在成功解决AttributeError: module ‘keras‘ has no attribute ‘utils‘_new1998的博客-CSDN博客这篇博客中博主有提到如何解决这一问题,其中就是要把 更改成为 而博主不知道其中原因,原因其实是在TensorFlow 2.4及以上版本中, import keras 的方式已经被弃用,取而代之的是 import tensorflow.k

    2024年02月11日
    浏览(35)
  • AttributeError: module ‘torch‘ has no attribute ‘cuda‘

    系统环境是Linux ,显卡:nvida-T4。 看了下原因为没有装pytorch。 (印象中是装了的不知道什么时候这台服务器没有了。。) 解决方案:到pytorch官网上找到对应的cuda版本的pytorch安装即可 Previous PyTorch Versions | PyTorch 比如我的是cuda 10.2(使用nvcc -V命令查看) 那么就是使用以下命

    2024年02月12日
    浏览(48)
  • AttributeError: module ‘numpy‘ has no attribute ‘float‘

    出现这个问题的原因是:从numpy1.24起删除了numpy.bool、numpy.int、numpy.float、numpy.complex、numpy.object、numpy.str、numpy.long、numpy.unicode类型的支持。解决上诉问题主要有两种方法: 安装numpy1.24之前的版本 可以用python内置类型或者np.ndarray类型替换: np.float替换为float或者np.float64/np.fl

    2024年02月11日
    浏览(31)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包