【发布时间】:2017-05-19 02:30:34
【问题描述】:
我正在尝试绘制一个二分图,左侧节点的颜色与右侧节点的颜色不同。我正在使用 networkx 和 matplotlib 这样做。 给定一个二分图 [(1, 3), (2, 5), (3, 4)],我希望显示 [1,2,3],一侧是蓝色,一侧是 [4,5] 水色另一侧,边缘 (1, 3), (2, 5), (3, 4) 介于两者之间。 以下是我的代码。
import networkx as nx
import matplotlib.pyplot as plt
def draw_bipartite(edges_list):
left,right = set(),set()
for s,t in edges_list:
right.add(s)
left.add(t)
B = nx.Graph()
B.add_nodes_from(list(right), bipartite=0)
B.add_nodes_from(list(left), bipartite=1)
B.add_edges_from(edges_list)
nodecolor = []
for node in B.nodes():
a = 'blue' if node in list(right) else 'aqua'
nodecolor.append(a)
l,r = nx.bipartite.sets(B)
pos = {}
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,with_labels = True,node_color=nodecolor)
plt.show()
draw_bipartite([(1, 3), (2, 5), (3, 4)])
在输出中,组 [1,2,3] 不保留在左侧,我如何将其保留在左侧以及蓝色?
【问题讨论】:
-
请在“我的输出图”链接中找到我的代码输出
-
顺便说一句,
node in list(right)效率很低。node in right好多了