【发布时间】:2018-07-01 00:33:33
【问题描述】:
我正在使用networkX生成树结构如下(我在关注answer of this question)。
import networkx as nx
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"
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')
但是,我收到一条错误消息,提示 AttributeError: module 'networkx' has no attribute 'write_dot'。我的 networkx 版本是 1.11(使用 conda)。我尝试了不同的hacks,但都没有奏效。
所以,我很想知道是否有另一种使用 networkx 绘制树结构的方法来获得类似于图中提到的输出。请告诉我。
【问题讨论】:
标签: python networkx graphviz pygraphviz