用你的格式来绘制它本身至少是另一个 SO 问题,所以我坚持按照你展示的方式大致解析数据,生成一个图表,然后做一个粗略的绘图。这是下面代码块的结果:
从绘图格式的角度来看,我所做的只是
- 包括节点标签
- 固定节点 B、C、F 和 J 的位置
- 稍微调整一下
nx.spring_layout() 参数,让情节更容易辨认。
import pandas as pd
import networkx as nx
d = {'Group Name': {1: 'Alpha', 2: 'Beta', 3: 'Gamma', 4: 'Omega'}, 'Members': {1: 'A, B, C', 2: 'C, D, E, F', 3: 'F, G, H, I, J', 4: 'J, K, L,M,N, O'}, 'Weight': {1: 'W1', 2: 'W2', 3: 'W3', 4: 'W4'}}
df = pd.DataFrame.from_dict(d)
subgraphs = []
for record in df.to_records():
nodes = [node.strip() for node in record[2].split(",")]
subgraph = nx.complete_graph(nodes)
nx.set_edge_attributes(subgraph, record[3], name='Weight')
subgraphs.append(subgraph)
G = nx.compose_all(subgraphs)
node_types = {'A': 1, 'B': 1, 'C':2, 'D': 1, 'E': 2, 'F': 1, 'G': 1, 'H': 1, 'I': 2, 'J': 2, 'K': 1, 'L': 1, 'M': 2, 'N': 1, 'O': 1}
nx.set_node_attributes(G, node_types, name='Type')
pos_fixed = {'B': (1, 0),
'C': (2, 0),
'F': (3, 0),
'J': (4, 0),
'K': (5, 0)}
pos = nx.spring_layout(G, k=1.25, pos=pos_fixed, fixed=pos_fixed.keys(), seed=42)
nx.draw(G, pos, with_labels=True)