AttributeError:模块“backend_interagg”没有属性“FigureCanvas”

Flo*_*low 4 python matplotlib networkx

我正在使用 matplotlib 版本 3.6.0 和 networkx 版本 2.6.3,由于某种原因,我的代码给出了AttributeError: module 'backend_interagg' has no attribute 'FigureCanvas'错误。

代码:


import networkx as nx
import matplotlib.pyplot as plt
import numpy as np
import matplotlib

G = nx.DiGraph()
nodes = np.arange(0, 8).tolist()
G.add_nodes_from(nodes)
G.add_edges_from([(0, 1), (0, 2),
                  (1, 3), (1, 4),
                  (2, 5), (2, 6), (2, 7)])
pos = {0: (10, 10),
       1: (7.5, 7.5), 2: (12.5, 7.5),
       3: (6, 6), 4: (9, 6),
       5: (11, 6), 6: (14, 6), 7: (17, 6)}
labels = {0: "CEO",
          1: "Team A Lead", 2: "Team B Lead",
          3: "Staff A", 4: "Staff B",
          5: "Staff C", 6: "Staff D", 7: "Staff E"}
nx.draw_networkx(G, pos=pos, labels=labels, arrows=True,
                 node_shape="s", node_color="white")
plt.title("Organogram of a company.")
plt.savefig("Output/plain organogram using networkx.jpeg",
            dpi=300)
Run Code Online (Sandbox Code Playgroud)

完整错误消息:

C:\Users\Flow\AppData\Local\Programs\Python\Python39\python.exe C:/Users/Flow/Shirt/Test.py
Traceback (most recent call last):
  File "C:\Users\Flow\Shirt\Test.py", line 21, in <module>
    nx.draw_networkx(G, pos=pos, labels=labels, arrows=True,
  File "C:\Users\Flow\AppData\Local\Programs\Python\Python39\lib\site-packages\networkx\drawing\nx_pylab.py", line 333, in draw_networkx
    draw_networkx_nodes(G, pos, **node_kwds)
  File "C:\Users\Flow\AppData\Local\Programs\Python\Python39\lib\site-packages\networkx\drawing\nx_pylab.py", line 445, in draw_networkx_nodes
    ax = plt.gca()
  File "C:\Users\Flow\AppData\Local\Programs\Python\Python39\lib\site-packages\matplotlib\pyplot.py", line 2225, in gca
    return gcf().gca()
  File "C:\Users\Flow\AppData\Local\Programs\Python\Python39\lib\site-packages\matplotlib\pyplot.py", line 830, in gcf
    return figure()
  File "C:\Users\Flow\AppData\Local\Programs\Python\Python39\lib\site-packages\matplotlib\_api\deprecation.py", line 454, in wrapper
    return func(*args, **kwargs)
  File "C:\Users\Flow\AppData\Local\Programs\Python\Python39\lib\site-packages\matplotlib\pyplot.py", line 771, in figure
    manager = new_figure_manager(
  File "C:\Users\Flow\AppData\Local\Programs\Python\Python39\lib\site-packages\matplotlib\pyplot.py", line 346, in new_figure_manager
    _warn_if_gui_out_of_main_thread()
  File "C:\Users\Flow\AppData\Local\Programs\Python\Python39\lib\site-packages\matplotlib\pyplot.py", line 336, in _warn_if_gui_out_of_main_thread
    if (_get_required_interactive_framework(_get_backend_mod()) and
  File "C:\Users\Flow\AppData\Local\Programs\Python\Python39\lib\site-packages\matplotlib\pyplot.py", line 206, in _get_backend_mod
    switch_backend(dict.__getitem__(rcParams, "backend"))
  File "C:\Users\Flow\AppData\Local\Programs\Python\Python39\lib\site-packages\matplotlib\pyplot.py", line 266, in switch_backend
    canvas_class = backend_mod.FigureCanvas
AttributeError: module 'backend_interagg' has no attribute 'FigureCanvas'

Process finished with exit code 1
Run Code Online (Sandbox Code Playgroud)

Flo*_*low 9

这是一个相当常见的问题,有很多原因(已编辑)tldr matplotlib 找不到支持画布绘图的后端这通常发生在 OSX(由于 OSX 处理应用程序的方式,tkinter 可能无法链接)或 linux(tkinter 可能不会链接)被安装,因为它是单独提供的,而不是默认情况下)尝试手动设置后端,因为我在 Windows 上,使用matplotlib.use('TkAgg')