将边的二部列表转换为NetworkX中的图,其中每组节点都有重叠的标签,可以通过以下步骤实现:
import networkx as nx
G = nx.DiGraph()
edges = [(1, 'A'), (1, 'B'), (2, 'B'), (2, 'C'), (3, 'C'), (3, 'D')]
for edge in edges:
group, label = edge
G.add_edge(group, label)
node_labels = {'A': 'Group 1', 'B': 'Group 1', 'C': 'Group 2', 'D': 'Group 2'}
pos = nx.spring_layout(G)
nx.draw_networkx(G, pos, labels=node_labels, with_labels=True)
完整代码如下:
import networkx as nx
G = nx.DiGraph()
edges = [(1, 'A'), (1, 'B'), (2, 'B'), (2, 'C'), (3, 'C'), (3, 'D')]
for edge in edges:
group, label = edge
G.add_edge(group, label)
node_labels = {'A': 'Group 1', 'B': 'Group 1', 'C': 'Group 2', 'D': 'Group 2'}
pos = nx.spring_layout(G)
nx.draw_networkx(G, pos, labels=node_labels, with_labels=True)
这样就可以将边的二部列表转换为NetworkX中的图,并且每组节点都有重叠的标签。关于NetworkX的更多信息和使用方法,可以参考腾讯云的相关产品介绍链接:NetworkX产品介绍。
领取专属 10元无门槛券
手把手带您无忧上云