您可以使用 pyvis 包在静态 HTML 文件中获取交互式图表。
import networkx as nx
from pyvis.network import Network
# Create directed graph with networkx
G = nx.DiGraph()
G.add_nodes_from([
('1', {'namespace': ['1n'], 'workload': {1: '1w'}}),
('2', {'namespace': ['2n'], 'workload': '2w'}),
('3', {'namespace': ['3n'], 'workload': '3w'}),
])
G.add_edges_from([
('1', '2', {'weight': 1}),
('2', '1', {'weight': 1}),
('2', '3', {'weight': 5}),
])
# Plot with pyvis
net = Network(
directed = True,
select_menu = True, # Show part 1 in the plot (optional)
filter_menu = True, # Show part 2 in the plot (optional)
)
net.show_buttons() # Show part 3 in the plot (optional)
net.from_nx(G) # Create directly from nx graph
net.show('test.html')
你可能需要仔细研究一下。我想说
pyvis
不太好。除了整数或字符串之外,它不能将任何对象作为节点。非常严格。
这是plotly 的文档 https://plotly.com/python/network-graphs/