简体   繁体   中英

How to change node positions in networkx graph?

I want to change node positions in a networkx.DiGraph based on a Dictionary of values.

My attempt:

1)First I generate a dictionary with Keys as Node names and Value as Tuples of X and Y coordinates and this is my result:

{'Start': (2, 2), 'S0': (0, 0), 'S1': (1, 0), 'S2': (2, 0), 'S3': (3, 0), 'S4': (4, 0), 'S5': (5, 0), 'S6': (6, 0), 'S7': (7, 0), 'S8': (8, 0), 'S9': (9, 0), 'S10': (10, 0), 'S11': (11, 0), 'S12': (12, 0), 'S13': (13, 0), 'S14': (14, 0), 'S15': (15, 0), 'S16': (16, 0), 'S17': (17, 0), 'S18': (18, 0), 'S19': (19, 0), 'S20': (20, 0), 'S21': (21, 0), 'S22': (22, 0), 'S23': (23, 0), 'S24': (24, 1), 'S25': (25, 1), 'S26': (26, 2), 'S27': (27, 2), 'S28': (28, 2), 'S29': (29, 3), 'S30': (30, 3), 'S31': (31, 3), 'S32': (32, 3), 'S33': (33, 3), 'S34': (34, 3), 'S35': (35, 3), 'S36': (36, 3), 'S37': (37, 3), 'S38': (38, 3), 'S39': (39, 3), 'S40': (40, 3), 'S41': (41, 3), 'S43': (43, 3), 'S44': (44, 3), 'S45': (45, 3), 'S47': (47, 3), 'S48': (48, 3), 'S49': (49, 3), 'S50': (50, 3), 'S51': (51, 3), 'S42': (42, 3), 'S46': (46, 3), 'Finish': (2, 2)}

2)Then I generate Nodes from list and add it to graph using G.add_nodes_from(nodes) (down below is the nodes list):

['Start', 'S0', 'S1', 'S2', 'S3', 'S4', 'S5', 'S6', 'S7', 'S8', 'S9', 'S10', 'S11', 'S12', 'S13', 'S14', 'S15', 'S16', 'S17', 'S18', 'S19', 'S20', 'S21', 'S22', 'S23', 'S24', 'S25', 'S26', 'S27', 'S28', 'S29', 'S30', 'S31', 'S32', 'S33', 'S34', 'S35', 'S36', 'S37', 'S38', 'S39', 'S40', 'S41', 'S43', 'S44', 'S45', 'S47', 'S48', 'S49', 'S50', 'S51', 'S42', 'S46', 'Finish']

3)Last but not least I generate the edges (data is too big to put here).

4)Then I put it all together according to this answer :

G = nx.DiGraph()
G.add_nodes_from(nodes) #Add nodes
#G.add_edges_from(edges) #DONT' use this line of code for reproduction
for node, pos in positions.items(): #Add Node positions
    G.nodes[node]['pos'] = pos

5)When I then plot the graph this is what I get:

nx.draw_shell(G, with_labels = True, node_size = 500)

在此处输入图像描述

What's the problem?

  1. Well as you can see the positions are not what I defined them to be.
  2. Weird think is that when I print out the attributes of nodes with this command: pos = nx.get_node_attributes(G,'pos') . I can see all my positions in the right place.
  3. How can I get the graph to plot Nodes in the desired positions?

Edit:

  • Changing the command from nx.draw_shell to nx.draw doesn't seem to help.

You can pass the position dictionary (keys are nodes, values are 2-d coordinates) to the more general draw_networkx function using the pos argument:

nx.draw_networkx(G,pos= positions,with_labels=True,node_color=node_color,node_size=500)

Note that the nx.draw_shell will layout your network according to a shell layout algorithm.

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