Networkx node sizes

2019-07-21 23:39发布

问题:

I am creating a networkX graph with the node sizes correlating to nodes. When I create the graph, the node sizes don't correspond correctly with the size list passed to the graph. Can anyone help? Here is the code I'm using:

import networkx as nx

G = nx.Graph()
nodelist = []
edgelist = []
sizes = []
for i in Results[0]:
    if i > 0.10 and i != 1:
        Normnodesize = (i/Maxcs)*800
        Findnode = np.where(Results[0]==i)
        nodelist.append(str(Findnode[0][0]))
        edgelist.append((0,str(Findnode[0][0])))
        sizes.append(int(Normnodesize))

G.add_nodes_from(nodelist)
G.add_edges_from(edgelist)

pos = nx.fruchterman_reingold_layout(G)

nx.draw_networkx(G,pos,node_size=sizes)

If you look at the nodes, edges, node weight the print out looks like:

   16 (0, '16') 608
   38 (0, '38') 732
   55 (0, '55') 549
   63 (0, '63') 800
   66 (0, '66') 559
  106 (0, '106') 693
  117 (0, '117') 476
  124 (0, '124') 672
  130 (0, '130') 482
  143 (0, '143') 518

The node sizes in the list and the node sizes depicted are not the same and I can't figure out why.

Thanks everyone!

回答1:

networkx stores nodes in a dict. When it draws them, it basically looks at the keys in the order python gives. There's no guarantee that's the same order that you've put their weights into sizes.

So try:

nx.draw_networkx(G,pos,nodelist = nodelist, node_size=sizes)