G = nx.read_edgelist('file.txt', delimiter="\t")
aux = G.edges(data=True)
B = nx.Graph()
B.add_nodes_from(list(employees), bipartite=0)
B.add_nodes_from(list(movies), bipartite=1)
B.add_edges_from(aux)
%matplotlib notebook
import [matplotlib][1].pyplot as plt
plt.figure()
edges = B.edges()
print(edges)
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_networkx(B, pos=pos, edges=edges)
plt.show()
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()
关键是为nx.draw{}参数创建dict,即:
A dictionary with nodes as keys and positions as values.
nx.draw_networkx(
B,
pos = nx.drawing.layout.bipartite_layout(B, B_first_partition_nodes),
width = edge_widths*5) # Or whatever other display options you like
另一个示例,将图与二部图相结合:
您可以这样做,在特定的
x
坐标处从每个分区绘制节点:关键是为}参数创建
nx.draw
{dict
,即:见the docs
NetworkX已经有了一个功能来实现这一点
它被称为networkx.drawing.layout.bipartite_layout
您可以使用它生成字典,该字典通过
pos
参数提供给绘图函数,如nx.draw
,如下所示:其中
B
是完整的二部图(表示为常规的networkx图),而B_first_partition_nodes
是希望放置在第一个分区中的节点这将生成一个数字位置字典,并将其传递给绘图函数的
pos
参数。也可以指定布局选项,请参见main page强制性示例输出:
相关问题 更多 >
编程相关推荐