NetworkX中的二部图 [英] Bipartite graph in NetworkX
本文介绍了NetworkX中的二部图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
B.add_nodes_from(a, bipartite=1)
B.add_nodes_from(b, bipartite=0)
nx.draw(B, with_labels = True)
plt.savefig("graph.png")
我得到下图.如何使它看起来像适当的二部图?
I am getting the following figure. How can I make it look like a proper bipartite graph?
推荐答案
您可以执行以下操作,从每个分区的特定x
坐标处绘制节点:
You could do something like this, to draw nodes from each partition at a particular x
coordinate:
X, Y = bipartite.sets(B)
pos = dict()
pos.update( (n, (1, i)) for i, n in enumerate(X) ) # put nodes from X at x=1
pos.update( (n, (2, i)) for i, n in enumerate(Y) ) # put nodes from Y at x=2
nx.draw(B, pos=pos)
plt.show()
关键在于为nx.draw
pos
参数创建dict
,即:
The key is creating the dict
for the the nx.draw
pos
parameter, which is:
以节点为键,位置为值的字典.
A dictionary with nodes as keys and positions as values.
请参见文档.
这篇关于NetworkX中的二部图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文