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')


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')

Aric Jul 14 2018-12-12T00: 00Z
source share