2014-11-23 93 views
7
B.add_nodes_from(a, bipartite=1) 
B.add_nodes_from(b, bipartite=0) 
nx.draw(B, with_labels = True) 
plt.savefig("graph.png") 

我收到下图。我怎样才能让它看起来像一个正确的二分图?NetworkX中的双向图形

My graph

回答

11

你可以做这样的事情,在特定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() 

bipartite-graph

的关键是创建dict为的nx.drawpos参数,即:

一个包含节点作为键和位置作为值的字典。

请参阅the docs