二分图问题

问题描述

这是我的代码...我不知道为什么情节显示这样

temp

为什么结果看起来不对?为什么不分别显示2组

enter image description here

如果我想画这样的图形...我该怎么办?如何使用二部布局,谁能帮我写下这段代码

enter image description here

解决方法

试试这个:

import networkx as nx
import matplotlib.pyplot as plt
import random
from networkx.algorithms import bipartite
B = nx.Graph()
n = [1,2,3,4]
l = [*'abc']
B.add_nodes_from(n,bipartite=0)
B.add_nodes_from(l,bipartite=1)
B.add_edges_from([(1,"a"),(1,"b"),(2,"c"),(3,(4,"a")])
pos = dict()
pos.update( (n,i)) for i,n in enumerate(n) ) 
pos.update( (n,n in enumerate(l) ) 

nx.draw_networkx(B,pos=pos)
nx.draw_networkx_nodes(B,pos=pos,nodelist=n,node_color='G')
nx.draw_networkx_nodes(B,node_shape='s',nodelist=l,node_color='r')

nx.draw_networkx_labels(B,pos,font_color='white',labels={i:i for i in l})

plt.axis('off')
plt.show(B)

输出:

enter image description here