使用Python中的networkX软件包绘制图形分区 [英] Drawing a graph partition with the networkX package in Python
本文介绍了使用Python中的networkX软件包绘制图形分区的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个图形对象 G
,其节点从 0
到 n-1
,并且有两个列表 L1
L2
是 G
节点的分区.我想以这样的方式绘制 G
:将节点分为两个块:一个相对于 L1
,而另一个相对于 L2
.图片的目的应该是证明 L1
和 L2
之间的联系.您能帮我执行此任务吗?
I have a graph object G
with nodes from 0
to n-1
and two lists L1
L2
which are a partition of the nodes of G
. I'd like to draw G
in such a way that the nodes result divided in two blocks: one relative to L1
and the other relative to L2
. The aim of the picture should be to evidentiate the connections between L1
and L2
. Can you help me do perform this task?
非常感谢.
推荐答案
分别绘制每个图形,并使用不相交的乘积生成显示边缘的新图形:
Draw each graph separately and use a disjoint product to generate a new graph that shows the edges:
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()
这篇关于使用Python中的networkX软件包绘制图形分区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文