B.add_nodes_from(a, bipartite=1)
B.add_nodes_from(b, bipartite=0)
nx.draw(B, with_labels = True)
plt.savefig("graph.png")
我得到下图。我怎样才能使它看起来像一个正确的二分图?
最佳答案
您可以执行类似的操作,从特定 x
处的每个分区绘制节点。坐标:
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()
关键是创建dict
对于nx.draw
pos
参数,即:
block 引用>A dictionary with nodes as keys and positions as values.
参见the docs .
关于python - NetworkX 中的二分图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27084004/