从python tup绘制NetworkX图形

2024-10-03 17:14:54 发布

您现在位置:Python中文网/ 问答频道 /正文

在下面我的新元组中:

我怎样才能使编号最高的“字符串”成为头节点,并将其他字符串名称附加到其编号的width=。在

new = (('COCH', 8), ('CAB', 4), ('VSNL', 7), ('ZNRF', 8), ('SLC12A1', 4), 
('APC', 16), ('LOC', 8), ('TRPM', 4), ('TNFRSF', 22))

在我上面的元组中,如何将最大数(22)“TNFRSF”作为节点,所有其他字符串都附加到该节点。连接宽度为其各自的数量。例如,“COCH”连接到宽度为8的“TNFRSF”节点。在


Tags: 字符串名称new宽度节点widthloc编号
1条回答
网友
1楼 · 发布于 2024-10-03 17:14:54
import networkx as nx

new = (('COCH', 8), ('CAB', 4), ('VSNL', 7), ('ZNRF', 8), ('SLC12A1', 4), ('APC', 16), ('LOC', 8), ('TRPM', 4), ('TNFRSF', 22))
children = sorted(new, key=lambda x: x[1])
parent = children.pop()[0]

G = nx.Graph()
for child, weight in children: G.add_edge(parent, child, weight=weight)
width = list(nx.get_edge_attributes(G, 'weight').values())
nx.draw_networkx(G, width=width)

相关问题 更多 >