I am new to Networkx and trying to figure out how to use the spring layout but applying different edge values (i.e., different distances between nodes) between nodes rather than the same edge value.
Essentially, I want a graph that tries to maintain a predefined set of node-node distances (likely using a spring layout to find some local minimum) with certain edges having a higher weight than others. However, the Networkx documentation suggests that all of the edges will have the same weight.
Additionally, on a simple case of drawing the spring_layout graph, I noticed that the resulting graph changes conformation each time I run it. Is there a way to get the same graph back (i.e., setting some random seed)?
import networkx as nx
G = nx.path_graph(5)
nx.draw(G)
You can accomplish what you're after (almost).
1) You can predefine some weights that will affect the node distances. (but you can't specify the distance directly)
2) You can feed an initial position into the spring_layout algorithm, which will result in a consistent final output. (You can even specify certain nodes that aren't allowed to change position if you want that as well). Or you can assign a seed to the random number generator used by nx.spring_layout
using the optional argument seed
.
import networkx as 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 as plt
plt.savefig('test.png')
Documentation is available here. The source code can be found here.
Look at nx.draw if you want rid of axes.
Note that there are other ways to add weighted edges than just what I've done.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With