Is there a way to guarantee hierarchical inference from NetworkX?

I am trying to create a block diagram of a tree structure. I was able to create representative graphs using networkx, but I need to show the structure of the tree when plotting the graph. I use matplotlib.pylab to plot the graph.

I need to show the data in a structure similar to what is shown here . Although I have no subgraphs.

How can I guarantee such a structure?

Examples of unbelievers:

Various NetworkX layouts

I managed to show graphs with pylab and graphviz, but I do not offer the tree structure I'm looking for. I tried every networkx layout that can offer, but none of them show hierarchy . I just don't know what options / mode to give it OR if I need to use weight. Any suggestions would help the bunch.

@jterrace:

Here is a rough outline of what I used to create the stories above. I added a few shortcuts, but also the same.

import networkx as nx import matplotlib.pyplot as plt G = nx.Graph() G.add_node("ROOT") for i in xrange(5): G.add_node("Child_%i" % i) G.add_node("Grandchild_%i" % i) G.add_node("Greatgrandchild_%i" % i) G.add_edge("ROOT", "Child_%i" % i) G.add_edge("Child_%i" % i, "Grandchild_%i" % i) G.add_edge("Grandchild_%i" % i, "Greatgrandchild_%i" % i) plt.title("draw_networkx") nx.draw_networkx(G) plt.show() 
+45
python networkx
Jul 13 '12 at 23:32
source share
2 answers

If you use a directed graph, then the Graphviz dot layout will do something like a tree with a tree. Below is a code similar to the solutions above showing how to do this

 import networkx as nx import matplotlib.pyplot as plt G = nx.DiGraph() G.add_node("ROOT") for i in xrange(5): G.add_node("Child_%i" % i) G.add_node("Grandchild_%i" % i) G.add_node("Greatgrandchild_%i" % i) G.add_edge("ROOT", "Child_%i" % i) G.add_edge("Child_%i" % i, "Grandchild_%i" % i) G.add_edge("Grandchild_%i" % i, "Greatgrandchild_%i" % i) # write dot file to use with graphviz # run "dot -Tpng test.dot >test.png" nx.write_dot(G,'test.dot') # same layout using matplotlib with no labels plt.title('draw_networkx') pos=nx.graphviz_layout(G, prog='dot') nx.draw(G, pos, with_labels=False, arrows=False) plt.savefig('nx_test.png') 

Graphviz output

NetworkX / Matplotlib output

UPDATED

Below is the version updated for networkx-2.0 (and with upcoming arrows for networkx-2.1 networkx-2.1 too).

 import networkx as nx from networkx.drawing.nx_agraph import write_dot, graphviz_layout import matplotlib.pyplot as plt G = nx.DiGraph() G.add_node("ROOT") for i in range(5): G.add_node("Child_%i" % i) G.add_node("Grandchild_%i" % i) G.add_node("Greatgrandchild_%i" % i) G.add_edge("ROOT", "Child_%i" % i) G.add_edge("Child_%i" % i, "Grandchild_%i" % i) G.add_edge("Grandchild_%i" % i, "Greatgrandchild_%i" % i) # write dot file to use with graphviz # run "dot -Tpng test.dot >test.png" write_dot(G,'test.dot') # same layout using matplotlib with no labels plt.title('draw_networkx') pos =graphviz_layout(G, prog='dot') nx.draw(G, pos, with_labels=False, arrows=True) plt.savefig('nx_test.png') 

enter image description here

+58
Jul 14 2018-12-12T00:
source share

You can use pygraphviz to get closer:

 >>> import pygraphviz >>> import networkx >>> import networkx as nx >>> G = nx.Graph() >>> G.add_node("ROOT") >>> for i in xrange(5): ... G.add_node("Child_%i" % i) ... G.add_node("Grandchild_%i" % i) ... G.add_node("Greatgrandchild_%i" % i) ... G.add_edge("ROOT", "Child_%i" % i) ... G.add_edge("Child_%i" % i, "Grandchild_%i" % i) ... G.add_edge("Grandchild_%i" % i, "Greatgrandchild_%i" % i) >>> A = nx.to_agraph(G) >>> A.layout('dot', args='-Nfontsize=10 -Nwidth=".2" -Nheight=".2" -Nmargin=0 -Gfontsize=8') >>> A.draw('test.png') 

Result: enter image description here

Note. I copied the graphviz options from the link above. I'm not sure why the fourth child is drawn on top, and not in a strictly vertical format. Maybe someone who knows more about Graphviz options can help with this.

+5
Jul 14 2018-12-12T00:
source share



All Articles