简体   繁体   中英

NetworkX graph plot from python tuple

In my tuple new below:

how can I make the "string" with highest number to be the head node and other string names attached to with width = to their number.

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

In my tuple above, how to have highest number(22) "TNFRSF" to be the node, to which all other strings are attached. With connection width being their respective number. For instance, 'COCH'connects to "TNFRSF" node with width = 8.

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM