如何使用python networkX包显示二部图? [英] How do display bipartite graphs with python networkX package?

查看:237
本文介绍了如何使用python networkX包显示二部图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一个人如何在python networkX包中显示一个二部图,一个类中的节点在左列中,而另一类中的节点在右列中?

How does one display a bipartite graph in the python networkX package, with the nodes from one class in a column on the left and those from the other class on the right?

我可以创建一个图形并像这样显示它

I can create a graph and display it like this

B = nx.Graph()
B.add_nodes_from([1,2,3,4], bipartite=0) # Add the node attribute "bipartite"
B.add_nodes_from(['a','b','c'], bipartite=1)
B.add_edges_from([(1,'a'), (1,'b'), (2,'b'), (2,'c'), (3,'c'), (4,'a')])
nx.draw(B)
plt.show()

但是我想在列的左侧保留节点1,2,3,4,在右边的列保留节点'a','b','c',并且它们之间有边.

But I want nodes 1,2,3,4 on the left in a column and the nodes 'a','b','c' in a column on the right, with edges going between them.

推荐答案

您需要自行设置每个节点的位置:

You need to set the positions for each node by yourself:

B = nx.Graph()
B.add_nodes_from([1,2,3,4], bipartite=0) # Add the node attribute "bipartite"
B.add_nodes_from(['a','b','c'], bipartite=1)
B.add_edges_from([(1,'a'), (1,'b'), (2,'b'), (2,'c'), (3,'c'), (4,'a')])

# Separate by group
l, r = nx.bipartite.sets(B)
pos = {}

# Update position for node from each group
pos.update((node, (1, index)) for index, node in enumerate(l))
pos.update((node, (2, index)) for index, node in enumerate(r))

nx.draw(B, pos=pos)
plt.show()

这篇关于如何使用python networkX包显示二部图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆