python - Networkx Spring Layout with Different Edge Values -


i new networkx , trying figure out how use spring layout applying different edge values (i.e., different distances between nodes) between nodes rather same edge value.

essentially, want graph tries maintain predefined set of node-node distances (likely using spring layout find local minimum) edges having higher weight others. however, networkx documentation suggests of edges have same weight.

additionally, on simple case of drawing spring_layout graph, noticed resulting graph changes conformation each time run it. there way same graph (i.e., setting random seed)?

import networkx nx g = nx.path_graph(5) nx.draw(g)  

you can accomplish you're after (almost).

1) can predefine weights affect node distances. (but can't specify distance directly)

2) can feed initial position spring_layout algorithm, result in consistent final output. (you can specify nodes aren't allowed change position if want well).

import networkx nx  g = nx.graph() g.add_edges_from([(1,2, {'myweight':20}), (2,3,{'myweight':0.1}),                    (1,4,{'myweight':1}), (2,4,{'myweight':50})]) initialpos = {1:(0,0), 2:(0,3), 3:(0,-1), 4:(5,5)} pos = nx.spring_layout(g,weight='myweight', pos = initialpos)  nx.draw_networkx(g,pos)  import pylab plt plt.savefig('test.png') 

enter image description here

documentation available here. source code can found here.

look @ nx.draw if want rid of axes.

note there other ways add weighted edges i've done.


Comments

Popular posts from this blog

c++ - No viable overloaded operator for references a map -

java - Custom OutputStreamAppender not run: LOGBACK: No context given for <MYAPPENDER> -

java - Cannot secure connection using TLS -