Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retrieving coordinates of plotted NetworkX graph nodes

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')

enter image description here

like image 919
Tatsuya Yokota Avatar asked Sep 17 '25 09:09

Tatsuya Yokota


1 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)}
like image 78
Aric Avatar answered Sep 20 '25 00:09

Aric