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

NetworkX图保存为Cytoscape格式后Pyvis加载报错求助

解决从Cytoscape格式加载NetworkX图后Pyvis可视化报错问题

问题原因

通过nx.cytoscape_data保存NetworkX图,再用nx.cytoscape_graph加载时,生成的图中每条边的属性字典会额外包含source和target字段。而Pyvis的from_nx方法在调用add_edge时,已经通过位置参数传入了source和target节点,再通过**kwargs展开边属性时,会重复传入source和target参数,触发got multiple values for argument 'source'错误。

解决方案

方案1:清理边属性中的冗余字段

加载图后,手动移除每条边属性里的source和target键,避免参数冲突:

from pyvis.network import Network
import networkx as nx
import json

# 生成图(原代码不变)
nx_graph = nx.cycle_graph(10)
nx_graph.nodes[1]['title'] = 'Number 1'
nx_graph.nodes[1]['group'] = 1
nx_graph.nodes[3]['title'] = 'I belong to a different group!'
nx_graph.nodes[3]['group'] = 10
nx_graph.add_node(20, size=20, title='couple', group="Coppia")
nx_graph.add_node(21, size=15, title='couple', group="Coppia")
nx_graph.add_edge(20, 21, weight=5)
nx_graph.add_node("Rome 112", title='Home Street', weight=5)
nx_graph.add_node(25, size=25, label='lonely', title='lonely node', group=3)

# 保存为Cytoscape格式(原代码不变)
cyJson = nx.cytoscape_data(nx_graph)
with open("nx.cyjs", 'w') as outfile:
    json.dump(cyJson, outfile, indent=4)

# 原可视化(不变)
nt1 = Network('500px', '500px', select_menu=True, filter_menu=True)
nt1.from_nx(nx_graph)
nt1.show('nx-before.html', False)

# 加载并清理边属性
with open("nx.cyjs", 'r') as readfile:
    reloadGraph = nx.cytoscape_graph(json.load(readfile))

# 移除边属性中的source和target字段
for u, v, attrs in reloadGraph.edges(data=True):
    attrs.pop('source', None)
    attrs.pop('target', None)

# 重新可视化
nt2 = Network('500px', '500px', select_menu=True, filter_menu=True)
nt2.from_nx(reloadGraph)
nt2.show('nx-after.html', False)

方案2:改用NetworkX原生序列化方式(推荐)

放弃Cytoscape格式,使用NetworkX支持的pickle序列化,避免引入冗余属性:

from pyvis.network import Network
import networkx as nx
import pickle

# 生成图(原代码不变)
nx_graph = nx.cycle_graph(10)
nx_graph.nodes[1]['title'] = 'Number 1'
nx_graph.nodes[1]['group'] = 1
nx_graph.nodes[3]['title'] = 'I belong to a different group!'
nx_graph.nodes[3]['group'] = 10
nx_graph.add_node(20, size=20, title='couple', group="Coppia")
nx_graph.add_node(21, size=15, title='couple', group="Coppia")
nx_graph.add_edge(20, 21, weight=5)
nx_graph.add_node("Rome 112", title='Home Street', weight=5)
nx_graph.add_node(25, size=25, label='lonely', title='lonely node', group=3)

# 改用pickle保存图
with open("nx_graph.pkl", 'wb') as outfile:
    pickle.dump(nx_graph, outfile)

# 原可视化(不变)
nt1 = Network('500px', '500px', select_menu=True, filter_menu=True)
nt1.from_nx(nx_graph)
nt1.show('nx-before.html', False)

# 加载pickle格式的图
with open("nx_graph.pkl", 'rb') as readfile:
    reloadGraph = pickle.load(readfile)

# 重新可视化
nt2 = Network('500px', '500px', select_menu=True, filter_menu=True)
nt2.from_nx(reloadGraph)
nt2.show('nx-after.html', False)

方案对比

  • 方案1:保留Cytoscape格式的兼容性(可用于其他支持该格式的工具),但需要手动处理冗余属性。
  • 方案2:实现更简单,无需额外处理,加载效率更高,但保存的是二进制文件,无法直接阅读内容。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.29 12:47:27