2014-11-01 89 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()