Hierarchical fixed position output from NetworkX without graphviz?

I am trying to use Python to build a simple hierarchical tree. I am using the networkx module. I announced a simple graph

G=networkx.DiGraph() 

      

After adding nodes and edges to G, I tried using

nx.draw(G) 

      

or

nx.draw_networkx(G) 

      

... The hierarchy of the output graph is correct, but the position of the nodes appears randomly on the graph. Even worse, every time I run the script, the position of the node is different.

There is a solution in a similar question that requires the graphviz package.

pos=nx.graphviz_layout(G,prog='dot')
nx.draw(G,pos,with_labels=False,arrows=False)

      

Unfortunately I am not allowed to install graphviz. So I am looking for an alternative solution here. From graphviz's solution, it seems like it only needs to calculate the position of the node. Is it correct to say that as long as I list the coordinates of the draw command, I could build the node in the correct place?

thank

+3


source to share


1 answer


UPDATE (Apr 15, 2015) Look at my answer here for code that I think will do what you need.


So networkx doesn't make it particularly convenient to use graphviz graphics if you don't have a graphical interface, because it's better to use the graphviz algorithm if you are trying to reproduce a graphics format.

But, if you're willing to put in the effort to calculate the position for each node, it's easy (you guessed it). Create a dict expression where the position of the node should be.



pos = {}
for node in G.nodes():
    pos[node] = (xcoord,ycoord)

nx.draw(G,pos)

      

will do it where xcoord and ycoord are the coordinates you want.

Various build commands are described here

+3


source







All Articles