2014-11-01 71 views
2

我有節點的圖對象G0n-1和兩個列表L1L2它們是G節點的一個分區。我想畫G這樣一種方式,節點結果分爲兩個塊:一個相對於L1,另一個相對於L2。圖片的目的應該是證明L1L2之間的關係。你能幫我做這個任務嗎?繪製與networkX包圖分區在Python

非常感謝提前。

+0

你你嘗試過什麼?你能向我們展示一個你的代碼的例子或你想要繪製什麼? – Aric 2014-11-01 23:03:35

回答

1

分別繪製每個圖形,並使用不相交的產品,以生成新的圖,顯示的邊緣:

enter image description here

import networkx as nx 

n1, n2 = 10, 10 

# Define two random graphs 
g1 = nx.gnm_random_graph(n1,20) 
g2 = nx.gnm_random_graph(n2,20) 
pos1 = nx.graphviz_layout(g1,prog='dot') 
pos2 = nx.graphviz_layout(g2,prog='dot') 

# Shift graph2 
shift = 400 
for key in pos2: 
    pos2[key] = (pos2[key][0]+shift, pos2[key][1]) 

# Combine the graphs and remove all edges 
g12 = nx.disjoint_union(g1,g2) 
for i,j in g12.edges(): 
    g12.remove_edge(i,j) 

# Add the conjoined edges 
g12.add_edge(5, 7+n1) 
g12.add_edge(2, 3+n1) 
g12.add_edge(8, 7+n1) 

# Set the new pos for g12 
pos12 = pos1.copy() 
for node in pos2: 
    pos12[node+n2] = pos2[node] 

# Show the results, make the conjoined edges darker 

import pylab as plt 
nx.draw_networkx(g1,pos=pos1,alpha=0.5) 
nx.draw_networkx(g2,pos=pos2,alpha=0.5) 
nx.draw_networkx_edges(g12,pos=pos12,width=5) 
plt.axis('off') 
plt.show()