Graph vizualisation with edges length inversely proportional to edges weight

2,344 views
Skip to first unread message

G Declic

unread,
Jul 6, 2017, 7:12:46 AM7/6/17
to networkx-discuss
Hello,

I am totally new to networkx and  trying to use Networkx to visualize a simple Graph where I have nodes connected by weighted edges.

In my case, each edge weight represents the "connection strength"  between two nodes. The stronger the connection, the closest should the nodes be represented in 2D and 3D space.

So the edges length in the final drawing should be inversely proportional to the edge weight. The result should be islands of nodes with strong connections isolated from other islands or sinle nodes with weak connections.

I wish for a layout algorithm that respects this as much as possible, knowing that being limited to 2 dimensions it will not be always possible for all edges.

I have tried the spring_layout in the way copied below but it does not result in what I am expecting (playing with the iterations number is even seems 'random' to me.

The nodes are nicely distributed in space but weights do not seem to influence edges lengths. So the spring layout might not be the right one for my case ?

Help would be greatly appreciated.

Thank you

pos = nx.spring_layout(G, iterations=10,weight='weight')

labels = nx.get_edge_attributes(G, 'weight')

edges = G.edges()

nx.draw(G, pos, edges=edges,with_labels=True,font_size=12,edge_labels=labels)
nx.draw_networkx_edge_labels(G, pos, edge_labels=labels)


Leigh Manley

unread,
Jul 14, 2017, 7:22:57 AM7/14/17
to networkx-discuss
I have the same question

Daniel Schult

unread,
Jul 14, 2017, 8:06:27 AM7/14/17
to networkx...@googlegroups.com
Declic, (and Leigh)

You are using the spring_layout with the right syntax. Changing the iterations should determine the speed of calculation and how close to equilibrium the result becomes. Since initial positions are random I am not surprised that changing the iterations appears to give random results -- if you aren't letting it get to equilibrium and initial positions are random then positions are likely to appear random. 

You state that the weight values don't seem to do anything to the resulting distances between nodes. That may be related to the scaling of your weights. The algorithm does make large weight edges shorter and small weight edges longer, but smaller and larger are relative statements.  So if the weights are close to the same value you will not see this effect.  Try it on a small graph and use weights like 0.1, 1, 20, 50 and watch the effect.

If your weights do not vary sufficiently, you can scale them before sending to the routine.

--
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-discuss+unsubscribe@googlegroups.com.
To post to this group, send email to networkx-discuss@googlegroups.com.
Visit this group at https://groups.google.com/group/networkx-discuss.
For more options, visit https://groups.google.com/d/optout.

Leigh Manley

unread,
Jul 14, 2017, 2:31:29 PM7/14/17
to networkx-discuss
Is it possible to customize edge length while keeping triangle inequality? I've been trying to customize edge length in python (with networkx and pygraphviz/matplotlib) and then in matlab and it seems like it's not an option for a reason, but there are a few who say they can do it (https://stackoverflow.com/questions/1851296/specified-edge-lengths-on-networkx-igraph-python/1898456#1898456).

wrt Daniel's answer I'm not sure weight will influence the length with Declic's code. I changed my weights from x to 10**x and they're still all the same length: https://drive.google.com/file/d/0B_JDBrcvs5VcY25RYmFIZ1VQSkE/view?usp=sharing


On Thursday, July 6, 2017 at 6:12:46 AM UTC-5, G Declic wrote:

On Thursday, July 6, 2017 at 6:12:46 AM UTC-5, G Declic wrote:
On Thursday, July 6, 2017 at 6:12:46 AM UTC-5, G Declic wrote:
On Thursday, July 6, 2017 at 6:12:46 AM UTC-5, G Declic wrote:
Reply all
Reply to author
Forward
0 new messages