Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does networkx redraw my graph different each run?

I'd like to draw a graph with edges representing correlation coefficients between nodes. I have an edge for each unique relationship. The graph is different every time I rerun the following code. Is there a way to force one form of the graph? Also, not sure if this is even generating the graph correctly - please help with anything that looks wrong.

G = nx.Graph()
G.add_edge('A', 'B', weight=0.511012)
G.add_edge('A', 'C', weight=0.553063)
G.add_edge('A', 'D', weight=0.607859)
G.add_edge('A', 'E', weight=0.601554)
G.add_edge('A', 'F', weight=0.641796)

G.add_edge('B', 'C', weight=0.438743)
G.add_edge('B', 'D', weight=0.463087)
G.add_edge('B', 'E', weight=0.615150)
G.add_edge('B', 'F', weight=0.478853)

G.add_edge('C', 'D', weight=0.553063)
G.add_edge('C', 'E', weight=0.438743)
G.add_edge('C', 'F', weight=0.541893)

G.add_edge('D', 'E', weight=0.535331)
G.add_edge('D', 'F', weight=0.556995)

G.add_edge('E', 'F', weight=0.535446)

nx.draw(G, with_labels=True, node_color='orange', node_size=400, edge_color='black', linewidths=1, font_size=15)
plt.show()
like image 860
ACL Avatar asked Dec 12 '18 01:12

ACL


People also ask

Is NetworkX scalable?

Due to its dependence on a pure-Python "dictionary of dictionary" data structure, NetworkX is a reasonably efficient, very scalable, highly portable framework for network and social network analysis.

How many nodes can NetworkX handle?

For NetworkX, a graph with more than 100K nodes may be too large. I'll demonstrate that it can handle a network with 187K nodes in this post, but the centrality calculations were prolonged. Luckily, there are some other packages available to help us with even larger graphs.

What is spring layout NetworkX?

By default, the layout of the nodes and edges is automatically determined by the Fruchterman-Reingold force-directed algorithm [62] (called “spring layout” in NetworkX), which conducts a pseudo-physics simulation of the movements of the nodes, assuming that each edge is a spring with a fixed equilibrium distance.

What is a DiGraph NetworkX?

A DiGraph stores nodes and edges with optional data, or attributes. DiGraphs hold directed edges. Self loops are allowed but multiple (parallel) edges are not. Nodes can be arbitrary (hashable) Python objects with optional key/value attributes.


1 Answers

The default of nx.draw uses nx.spring_layout to set the positions of the nodes. Unless a seed is fed into nx.spring_layout, it starts from a random initial condition and then uses movements based on treating the edges as springs and the nodes as masses to reposition the nodes. Because of this random initial condition, the positions will be different each time.

Often however, we may want to use multiple drawing commands to draw the nodes or edges(for example if we want some nodes in one size or style and other nodes in another size or style). For that, nx.draw accepts an optional argument pos which is a dictionary whose keys are the nodes and whose values are 2-tuples giving their (x,y) coordinates. Networkx has several functions that assign positions using different rules, and as I said above, the default uses a random initial condition so will give different outputs each time.

In your case you want to be sure that the network is given the same position each time you run your code. So you should feed a seed to the command that sets the positions.

#code to generate graph G here.
my_pos = nx.spring_layout(G, seed = 100)
nx.draw(G, pos = my_pos, with_labels=True, node_color='orange', node_size=400, edge_color='black', linewidths=1, font_size=15)
plt.show()

It is possible to allow the weights of the edges to cause the spring layout to act as if the higher weights are stronger springs. Check the documentation of spring_layout for more detail.

like image 134
Joel Avatar answered Oct 13 '22 01:10

Joel