当前位置:   article > 正文

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

attributeerror: module 'backend_interagg' has no attribute 'figurecanvas

目录

1、问题展示

2.问题原因

3.问题解决方案

4、修改代码如下

4.1 修改前

4.2修改后

5、代码执行结果


1、问题展示

  1. Traceback (most recent call last):
  2. File "E:\workspace\python_project\aitest\test1.py", line 20, in <module>
  3. do_test()
  4. File "E:\workspace\python_project\aitest\test1.py", line 15, in do_test
  5. plt.plot(x, y, 'r-', linewidth=3)
  6. File "E:\workspace\python_project\aitest\venv\lib\site-packages\matplotlib\pyplot.py", line 3575, in plot
  7. return gca().plot(
  8. File "E:\workspace\python_project\aitest\venv\lib\site-packages\matplotlib\pyplot.py", line 2525, in gca
  9. return gcf().gca()
  10. File "E:\workspace\python_project\aitest\venv\lib\site-packages\matplotlib\pyplot.py", line 1000, in gcf
  11. return figure()
  12. File "E:\workspace\python_project\aitest\venv\lib\site-packages\matplotlib\pyplot.py", line 934, in figure
  13. manager = new_figure_manager(
  14. File "E:\workspace\python_project\aitest\venv\lib\site-packages\matplotlib\pyplot.py", line 464, in new_figure_manager
  15. _warn_if_gui_out_of_main_thread()
  16. File "E:\workspace\python_project\aitest\venv\lib\site-packages\matplotlib\pyplot.py", line 441, in _warn_if_gui_out_of_main_thread
  17. canvas_class = cast(type[FigureCanvasBase], _get_backend_mod().FigureCanvas)
  18. File "E:\workspace\python_project\aitest\venv\lib\site-packages\matplotlib\pyplot.py", line 280, in _get_backend_mod
  19. switch_backend(rcParams._get("backend")) # type: ignore[attr-defined]
  20. File "E:\workspace\python_project\aitest\venv\lib\site-packages\matplotlib\pyplot.py", line 343, in switch_backend
  21. canvas_class = module.FigureCanvas
  22. AttributeError: module 'backend_interagg' has no attribute 'FigureCanvas'. Did you mean: 'FigureCanvasAgg'?

2.问题原因

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

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

源码

  1. global _backend_mod
  2. # make sure the init is pulled up so we can assign to it later
  3. import matplotlib.backends
  4. if newbackend is rcsetup._auto_backend_sentinel:
  5. current_framework = cbook._get_running_interactive_framework()
  6. mapping = {'qt': 'qtagg',
  7. 'gtk3': 'gtk3agg',
  8. 'gtk4': 'gtk4agg',
  9. 'wx': 'wxagg',
  10. 'tk': 'tkagg',
  11. 'macosx': 'macosx',
  12. 'headless': 'agg'}
  13. if current_framework in mapping:
  14. candidates = [mapping[current_framework]]
  15. else:
  16. candidates = []
  17. candidates += [
  18. "macosx", "qtagg", "gtk4agg", "gtk3agg", "tkagg", "wxagg"]
  19. # Don't try to fallback on the cairo-based backends as they each have
  20. # an additional dependency (pycairo) over the agg-based backend, and
  21. # are of worse quality.
  22. for candidate in candidates:
  23. try:
  24. switch_backend(candidate)
  25. except ImportError:
  26. continue
  27. else:
  28. rcParamsOrig['backend'] = candidate
  29. return
  30. else:
  31. # Switching to Agg should always succeed; if it doesn't, let the
  32. # exception propagate out.
  33. switch_backend("agg")
  34. rcParamsOrig["backend"] = "agg"
  35. return

3.问题解决方案

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

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

backends的参数有如下

  1. if newbackend is rcsetup._auto_backend_sentinel:
  2. current_framework = cbook._get_running_interactive_framework()
  3. mapping = {'qt': 'qtagg',
  4. 'gtk3': 'gtk3agg',
  5. 'gtk4': 'gtk4agg',
  6. 'wx': 'wxagg',
  7. 'tk': 'tkagg',
  8. 'macosx': 'macosx',
  9. 'headless': 'agg'}
  10. if current_framework in mapping:
  11. candidates = [mapping[current_framework]]
  12. else:
  13. candidates = []
  14. candidates += [
  15. "macosx", "qtagg", "gtk4agg", "gtk3agg", "tkagg", "wxagg"]

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

4、修改代码如下

4.1 修改前

  1. import matplotlib.pyplot as plt
  2. def do_test():
  3. plt.plot([1, 2, 3, 4])
  4. plt.ylabel('some numbers')
  5. plt.show()
  6. if __name__ == '__main__':
  7. do_test()

4.2修改后

  1. import matplotlib
  2. import matplotlib.pyplot as plt
  3. matplotlib.use('TkAgg')
  4. def do_test():
  5. plt.plot([1, 2, 3, 4])
  6. plt.ylabel('some numbers')
  7. plt.show()
  8. if __name__ == '__main__':
  9. do_test()

5、代码执行结果

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

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/2023面试高手/article/detail/371846?site
推荐阅读
相关标签
  

闽ICP备14008679号