Bipartite graph in NetworkX

前端 未结 3 1615
攒了一身酷
攒了一身酷 2020-12-01 15:02
B.add_nodes_from(a, bipartite=1)
B.add_nodes_from(b, bipartite=0)
nx.draw(B, with_labels = True)  
plt.savefig(\"graph.png\")

I am getting the foll

3条回答
  •  伪装坚强ぢ
    2020-12-01 15:12

    NetworkX already has a function to do exactly this.

    Its called networkx.drawing.layout.bipartite_layout

    You use it to generate the dictionary that is fed to the drawing functions like nx.draw via the pos argument like so:

    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
    

    Where B is the full bipartite graph (represented as a regular networkx graph), and B_first_partition_nodes are the nodes you wish to place in the first partition.

    This generates a dictionary of numeric positions that is passed to the pos argument of the drawing function. You can specify layout options as well, see the main page.

    Obligatory example output:

提交回复
热议问题