2017-02-16 11 views
0

레벨이 나누어 진 그래프가 있습니다. :NetworkX : 그래프를 레이어로 그립니다.

ids : 0 - 100 are lowest level 
ids : 101 - 500 are level 2 
ids : 501 - 1500 are level 3 
and so on ... 

그래프가 강제로 레이어를 구성하는 계층에서 노드를 끌어 오도록하는 방법이 있습니까?

나는 오버 플로우 : 노드가 노드 ID에 따라 결정되는 계층 내 경우

없이 스택 할,하지만 당신은 몇 가지 아이디어가있는 경우는, 다른 조직의 원칙이 될 수 있습니다. 그 키 사전인가 반환 등 nx.spring_layout 등 레이아웃 기능을 ...

def plot(self): 
    plt.figure() 
    pos = nx.graphviz_layout(self.g,prog='dot') 
    nx.draw(self.g, pos, node_size=650, node_color='#ffaaaa') 

다섯 개 레이어 예

enter image description here

+0

을 생산? – edo

답변

1

:


지금까지 가능한 솔루션 같아이 노드이고 값은 2- 튜플 (좌표)입니다. 어떤 방법을, 당신은 다음이 좌표 추가로 조작 할 수 있습니다

In [101]: pos 
Out[101]: 
{(0, 0): array([ 0.70821816, 0.03766149]), 
(0, 1): array([ 0.97041253, 0.30382541]), 
(0, 2): array([ 0.99647583, 0.63049339]), 
(0, 3): array([ 0.86691957, 0.86393669]), 
(1, 0): array([ 0.79471631, 0.08748146]), 
(1, 1): array([ 0.71731384, 0.35520076]), 
(1, 2): array([ 0.69295087, 0.71089292]), 
(1, 3): array([ 0.63927851, 1.  ]), 
(2, 0): array([ 0.42228877, 0.  ]), 
(2, 1): array([ 0.33250362, 0.3165331 ]), 
(2, 2): array([ 0.31084694, 0.69246818]), 
(2, 3): array([ 0.34141212, 0.9952164 ]), 
(3, 0): array([ 0.16734454, 0.11357547]), 
(3, 1): array([ 0.01560951, 0.33063389]), 
(3, 2): array([ 0.  , 0.63044189]), 
(3, 3): array([ 0.12242227, 0.85656669])} 

을하시기 바랍니다 : 여기에 pos DICT의 모양에 대한 예제입니다. 예를 들어, xy 때문에 spring_layout 의해 반환 좌표를 0과 1 사이에있는 경우 레이어로 노드를 분리하는 y -coordinate 10 배 층 레벨 값을 추가 할 수

for node in pos: 
    level = node // nodes_per_layer 
    pos[node] += (0,10*level) 

import networkx as nx 
import matplotlib.pyplot as plt 

layers = 5 
nodes_per_layer = 3 
n = layers * nodes_per_layer 
p = 0.2 

G = nx.fast_gnp_random_graph(n, p, seed=2017, directed=True) 
pos = nx.spring_layout(G, iterations=100) 

for node in pos: 
    level = node // nodes_per_layer 
    pos[node] += (0,10*level) 

nx.draw(G, pos, node_size=650, node_color='#ffaaaa', with_labels=True) 
plt.show() 

당신이 원하는 결과를 예를 들어 이미지를 게시 할 수 enter image description here