Adjusting Node Positions in Networkx plot

1,504 views
Skip to first unread message

Sagnik BasuMallik

unread,
Apr 14, 2019, 5:18:03 PM4/14/19
to networkx-discuss
Is there a way to adjust positions of nodes in Networkx plot ? For example, as shown below, I am trying to colour communities of friends in one zone. However, the dark blue and the dark green regions seem to overlap (although they form their own individual community)?
Can I adjust the points in the plot?

Island_4Area_LoadGen.png

My code is as follows:
A = np.array(input_data) #input adjacency graph
for x in range(1,node+1):
for y in range(1,node+1):
if A[x][y] == 1:
G.add_edge(x, y, color='gray', weight=1) #W[x][y]

Z = np.array(node) # input of nodes
nodeZone = Z[:,0]
color_map = []
node_shape = []
for node in G:
print(node)
if nodeZone[node-1]== 1:
color_map.append('aqua')
# node_shape.append('\'o\',')
elif nodeZone[node-1]== 2:
color_map.append('lawngreen')
# node_shape.append('\'s\',')
elif nodeZone[node-1]== 3:
color_map.append('forestgreen')
# node_shape.append('\'v\',')
    else:
color_map.append('gray')

pos = nx.kamada_kawai_layout(G)
edges = G.edges()
colors = [G[u][v]['color'] for u,v in edges]
weights = [G[u][v]['weight'] for u,v in edges]
nx.draw(G, dim=2, pos=pos, edges=edges, edge_color=colors, width=weights,node_size=50,node_color=color_map, with_labels=True,font_size=6)
plt.show()


Dan Schult

unread,
Apr 15, 2019, 8:10:59 AM4/15/19
to networkx...@googlegroups.com
A short answer is that the node positions are held in your variable 'pos'.
That is a dict keyed by node to an (x,y) 2-tuple that represents the position of the node.

You can adjust the results of the layout routine as you like before drawing.

A longer answer is that you could layout each subgraph in a separate layout call using your choice of center and scale. Then combine the resulting pos-dicts into a single dict before sending to draw().  This is more involved and untested, but presumably would work.


--
You received this message because you are subscribed to the Google Groups "networkx-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to networkx-discu...@googlegroups.com.
To post to this group, send email to networkx...@googlegroups.com.
Visit this group at https://groups.google.com/group/networkx-discuss.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages