You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

matplotlib3.6.0运行networkx绘图报错backend_interagg无FigureCanvas属性

问题描述

运行环境: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)

完整报错信息如下:

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
原因说明

该错误由matplotlib渲染后端配置异常导致:backend_interagg是matplotlib的交互预览后端,当前环境中该后端安装不全或配置错误,无法正常加载所需的FigureCanvas组件。由于需求仅为保存图片到本地,不需要弹出交互预览窗口,直接切换为非交互后端即可解决。

解决方案
  • 方案1:代码内临时指定后端(推荐,无需修改全局配置)
    在导入matplotlib.pyplot之前,手动指定非交互后端Agg,修改后的代码开头如下:
import networkx as nx
import numpy as np
import matplotlib
# 先指定后端再导入pyplot
matplotlib.use('Agg')
import matplotlib.pyplot as plt

其余代码保持不变即可,额外注意确保保存路径中的Output文件夹提前创建,否则会触发路径不存在的报错。

  • 方案2:全局修改matplotlib配置
    找到matplotlib的配置文件matplotlibrc,将其中的backend参数修改为可用的后端(如Agg、TkAgg、Qt5Agg等),修改后所有matplotlib相关代码都会默认使用指定后端。

内容的提问来源于stack exchange,提问作者Flow

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.27 06:45:04