Retrieving coordinates of plotted NetworkX graph nodes
Question:
In the following code, I’m plotting an adjacency matrix in 2D as shown in the image below. Since networkx is able to plot the nodes, there must be information of the coordinates of the nodes, and I was wondering if there is any method to retrieve the two-dimensional coordinates of each node in the plotted graph. I have not had any luck with googling and looking at the documentation.
import networkx as nx
from networkx.drawing.nx_agraph import to_agraph
dt = [('len', float)]
A = np.array([(0, 5, 5, 5),
(0.3, 0, 0.9, 0.2),
(0.4, 0.9, 0, 0.1),
(1, 2, 3, 0)
])*10
A = A.view(dt)
G = nx.from_numpy_matrix(A)
G.draw('distances_1.png', format='png', prog='neato')
Answers:
Your code doesn’t run as written. But I think you just want the neato layout positions which you can get by calling graphviz_layout
(using pygraphviz).
The result is a dictionary with nodes as keys and positions as values.
In [1]: import networkx as nx
In [2]: import numpy as np
In [3]: from networkx.drawing.nx_agraph import graphviz_layout
In [4]: A = np.array([(0, 5, 5, 5),
...: (0.3, 0, 0.9, 0.2),
...: (0.4, 0.9, 0, 0.1),
...: (1, 2, 3, 0)
...: ])*10
In [5]: G = nx.from_numpy_matrix(A)
In [6]: pos = graphviz_layout(G, prog='neato')
In [7]: pos
Out[7]:
{0: (-42.946, -6.3677),
1: (42.991, 6.3533),
2: (6.3457, -42.999),
3: (-6.3906, 43.014)}
By default networkx.draw()
calculates the positions using networkx.spring_layout()
. You can use spring_layout()
, or any other layout function, to get the coordinates directly.
You can export these coordinates or pass them back to draw()
to use them in a plot.
import networkx
import matplotlib.pyplot as plt
G = networkx.petersen_graph()
pos = networkx.spring_layout(G)
networkx.draw(G, pos)
plt.show()
print(pos)
{0: array([-0.91403075, 0.80432224]),
1: array([-0.20318824, 0.06553106]),
...
9: array([1. , 0.70843343])}
In the following code, I’m plotting an adjacency matrix in 2D as shown in the image below. Since networkx is able to plot the nodes, there must be information of the coordinates of the nodes, and I was wondering if there is any method to retrieve the two-dimensional coordinates of each node in the plotted graph. I have not had any luck with googling and looking at the documentation.
import networkx as nx
from networkx.drawing.nx_agraph import to_agraph
dt = [('len', float)]
A = np.array([(0, 5, 5, 5),
(0.3, 0, 0.9, 0.2),
(0.4, 0.9, 0, 0.1),
(1, 2, 3, 0)
])*10
A = A.view(dt)
G = nx.from_numpy_matrix(A)
G.draw('distances_1.png', format='png', prog='neato')
Your code doesn’t run as written. But I think you just want the neato layout positions which you can get by calling graphviz_layout
(using pygraphviz).
The result is a dictionary with nodes as keys and positions as values.
In [1]: import networkx as nx
In [2]: import numpy as np
In [3]: from networkx.drawing.nx_agraph import graphviz_layout
In [4]: A = np.array([(0, 5, 5, 5),
...: (0.3, 0, 0.9, 0.2),
...: (0.4, 0.9, 0, 0.1),
...: (1, 2, 3, 0)
...: ])*10
In [5]: G = nx.from_numpy_matrix(A)
In [6]: pos = graphviz_layout(G, prog='neato')
In [7]: pos
Out[7]:
{0: (-42.946, -6.3677),
1: (42.991, 6.3533),
2: (6.3457, -42.999),
3: (-6.3906, 43.014)}
By default networkx.draw()
calculates the positions using networkx.spring_layout()
. You can use spring_layout()
, or any other layout function, to get the coordinates directly.
You can export these coordinates or pass them back to draw()
to use them in a plot.
import networkx
import matplotlib.pyplot as plt
G = networkx.petersen_graph()
pos = networkx.spring_layout(G)
networkx.draw(G, pos)
plt.show()
print(pos)
{0: array([-0.91403075, 0.80432224]), 1: array([-0.20318824, 0.06553106]), ... 9: array([1. , 0.70843343])}