0

I would like to add additional forces to networkx spring_layout.

I have a directected graph and I would like nodes to move to different sides according to the edges that they have. Nodes that have more outgoing edges should drift to nodes that have more ingoing edges should drift right. Another alternative would be. That these groups of nodes would drift towards each other, nodes with outgoing edges would get closer while nodes with ingoing edges would also get closer to each other.

I managed to look into to the source code of spring_layout of networkx http://networkx.lanl.gov/archive/networkx-0.37/networkx.drawing.layout-pysrc.html#spring_layout but everything there is just beyond my comprehension

G.DiGraph()
G.add_edges_from([(1,5),(2,5),(3,5),(5,6),(5,7)])

The layout should show edges 1,2,3 closer to each other, the same regarding 6 and 7.

I imagine, that I could solve this by adding invisible edges via using MultiDiGraph. I could count ingoing and outgoing edges of each node and add invisible edges that connect the two groups. However, I am very sure that there are better ways of solving the problem.

Aidis
  • 1,272
  • 4
  • 14
  • 31

1 Answers1

1

Adding weights into the mix would be a good way to group things (with those invisible nodes). But the layouts have no way of knowing left from right. To get the exact layout you want you could specify each point's x,y coordinates.

import networkx as nx
G=nx.Graph()

G.add_node(1,pos=(1,1))
G.add_node(2,pos=(2,3))
G.add_node(3,pos=(3,4))
G.add_node(4,pos=(4,5))
G.add_node(5,pos=(5,6))
G.add_node(6,pos=(6,7))
G.add_node(7,pos=(7,9))

G.add_edges_from([(1,5),(2,5),(3,5),(5,6),(5,7)])

pos=nx.get_node_attributes(G,'pos')
nx.draw(G,pos)
Back2Basics
  • 7,406
  • 2
  • 32
  • 45
  • Do you offer to create invisible nodes on left and right of screen and connect these to my nodes with invisible edges? This seems like an interesting technique, I wonder if it used anywhere already. – Aidis Jul 15 '14 at 13:48