welu
welu

Reputation: 339

How to affect positions to the graph nodes for visualization?

I have a graph that I read with Networkx from a Dotfile, and to visualize it I need to affect position (x,y) to each node. How can I do that ?

For visualization, I use Plotly offline mode on python3.6 , and for that I will need the positions for the edge and nodes. For now , I try giving random values and that seems to not being working at all

Basically, the code i use for plotting is this one : https://plot.ly/python/network-graphs/ the algorithm uses 'pos' from the random created Graph , but for me , I don't have thoses coordonates and I don't have any idea how to generate them.

Upvotes: 3

Views: 556

Answers (1)

vurmux
vurmux

Reputation: 10030

The problem you are trying to solve is known as graph layouts. There are some graph layout algorithms in networkx: networkx.drawing.layout. Here is the example:

import networkx as nx

G = nx.Graph()
G.add_nodes_from([1,2,3,4,5])
G.add_edges_from([(1,2),(2,3),(2,4),(4,5)])
nx.spring_layout(G)

Returns coordinates of all nodes:

{1: array([-0.73835686,  0.38769421]),
 2: array([-0.11386106,  0.22912486]),
 3: array([0.14264776, 0.81717097]),
 4: array([ 0.21475452, -0.43399004]),
 5: array([ 0.49481564, -1.        ])}

Upvotes: 2

Related Questions