NetworkX에서 계층 적 출력을 보장하는 방법이 있습니까?
트리 구조 의 흐름도를 생성합니다 . networkx 대표 그래프를로 만들 수 있었지만 플롯을 출력 할 때 트리 구조 를-display 할 방법이 필요합니다 . 그래프를 그리기 위해 matplotlib.pylab을 사용하고 있습니다.
여기 에 일치하는 구조로 데이터를 표시해야합니다 . 하위 그래프는 없지만.
그런 구조를 어떻게 보장 할 수 있습니까?
불신 튼튼 예 :
pylab 및 graphviz를 사용하여 그래프를 표시 할 수 있었지만 어느 쪽도 내가 찾고있는 트리 구조를 제공하지 않습니다. networkx가 제공해야하는 모든 레이아웃을 시도했지만 계층 구조를 표시하는 것은 아닙니다 . 어떤 옵션 / 모드 를 제공 해야하는지 또는 사용 해야하는지 잘 모르겠습니다 . 어떤 도움이 도움이 될 것입니다.
@jterrace :
다음은 위의 및 생성하는 데 존재적인 개요입니다. 몇 가지 레이블을 추가했지만 그 외에는 동일합니다.
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()
유 방향 그래프를 사용하는 경우 Graphviz 도트 레이아웃은 트리에서 원하는 작업을 수행합니다. 다음은이를 수행하는 방법을 수행하는 방법입니다.
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')
업데이트 됨
다음은 networkx-2.0 용으로 업데이트 된 버전입니다 (그리고 네트워크 x-2.1도 화살표를 그립니다).
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')
pygraphviz를 사용하여 가까이 다가 갈 수 있습니다.
>>> 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')
결과 :
참고 위에 게시 한 링크에서 graphviz 옵션을 복사했습니다. 4 번째 아이가 엄격하게 수직 형식이 아닌 맨 위에 그려지는 이유를 잘 모르겠습니다. Graphviz 옵션에 대해 더 많이 아는 사람이 도움을 줄 수 있습니다.
'ProgramingTip' 카테고리의 다른 글
Nuget 통합 대 업데이트 (0) | 2020.11.08 |
---|---|
gnuplot 파일에 대한 표준 파일 듣고가 있습니까? (0) | 2020.11.08 |
github의 git repo를 동일한 조직으로 복사 / 포크 (0) | 2020.11.08 |
IntelliJ IDEA 내에서 Groovy SDK 구성 (0) | 2020.11.08 |
C #에 변경 불가능한 사전을 제공하는 방법이 있습니까? (0) | 2020.11.08 |