Reputation: 371
import networkx as nx
%matplotlib inline
import matplotlib.pyplot as plt
G2=nx.DiGraph()
G2.add_node(900,pos=(0,0))
G2.add_node(901,pos=(1,0))
G2.add_node(902,pos=(0,1))
G2.add_node(903,pos=(1,1))
G2.add_node(904,pos=(0,-1))
nodePos = nx.circular_layout(G2)
print("nodePos : \n" , nodePos)
nx.draw_networkx(G2, with_labels = True)
plt.show()
The code above creates the graph shown below with the node positions:
nodePos :
{900: array([ 1., 0.]), 901: array([ 0.30901699, 0.95105652]), 902: array([-0.80901699, 0.58778525]), 903: array([-0.80901699, -0.58778525]), 904: array([ 0.30901699, -0.95105652])}
Questions:
a) Node 900 is given location of 0, 0 but x seems to be more than 0.8 for this node... Similarly, 904 is given y as -1 but the co-ordinate system does not show -1 anywhere.
b) nx.circular_layout() is supposed to return the location of nodes and that is what I printed in nodePos. But, I do not understand the values printed in nodePos. Why do I see decimal values and that too with negative values.
c) My intent is to get the correct location of nodes so that I can take it and do graph drawing at my end in Qt/Java etc....
Upvotes: 4
Views: 16203
Reputation: 8801
You need to add the pos
argument for nx.draw_networkx
graph, like this
import networkx as nx
%matplotlib inline
import matplotlib.pyplot as plt
G2=nx.DiGraph()
G2.add_node(900,pos=(0,0))
G2.add_node(901,pos=(1,0))
G2.add_node(902,pos=(0,1))
G2.add_node(903,pos=(1,1))
G2.add_node(904,pos=(0,-1))
nodePos = nx.circular_layout(G2)
print("nodePos : \n" , nodePos)
#('nodePos : \n', {904: array([1.00000000e+00, 2.38418583e-08]),
# 900: array([0.30901696, 0.95105658]), 901: array([-0.80901709, 0.58778522]),
# 902: array([-0.80901698, -0.58778535]), 903: array([ 0.30901711, -0.95105647])})
nx.draw_networkx(G2, with_labels = True,pos=nodePos)
plt.show()
You can read the documentation for pos
arguement here, it says that
pos (dictionary, optional) – A dictionary with nodes as keys and positions as values. If not specified a spring layout positioning will be computed.
Upvotes: 4