2017-02-16 124 views
0

我有一個在圖層上分割的圖形,即f.e. :NetworkX:在圖層中繪製圖形

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

是否有某種方法可以強制圖形繪製圖層中的節點,一個在另一個上面。

我想堆疊起來沒有溢出:)

在我的情況在該層中,節點是依賴於節點ID,但它可能是一些其他的組織原則,如果你有一些想法。


這到目前爲止似乎是可能的解決方案:

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

佈局功能,如nx.spring_layout,返回一個字典,它的鍵是節點,其值是2元組(座標)。這裏的pos字典可能看起來像一個例子:

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])} 

然後,您可以操縱這些座標進一步,任何方式都可以。例如,由於在 和x座標y通過spring_layout返回是0和1之間,則可以 10倍層等級值添加到y - 協調到節點分離成層:

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