0
votes

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.

1

1 Answers

1
votes
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)