Drawing a graph or a network from a distance matrix?

后端 未结 2 2081
滥情空心
滥情空心 2020-11-29 00:40

I\'m trying to plot/sketch (matplotlib or other python library) a 2D network of a big distance matrix where distances would be the edges of the sketched network and the line

2条回答
  •  粉色の甜心
    2020-11-29 01:13

    You can use the networkx package, that work perfectly with this kind of problems. Adjust your matrix to remove a simple numpy array like this:

    DistMatrix =array([[0,      0.3,    0.4,    0.7],
    [0.3,    0,      0.9,    0.2],
    [0.4,    0.9,    0,      0.1],
    [0.7,    0.2,    0.1,    0] ])
    

    then import networkx and use it

    import networkx as nx
    G = G=nx.from_numpy_matrix(DistMatrix)
    nx.draw(G)
    

    if you want to draw a weighted version of the graph, you have to specify the color of each edge (at least, I couldn't find a more automated way to do it):

    nx.draw(G,edge_color = [ i[2]['weight'] for i in G.edges(data=True) ], edge_cmap=cm.winter )
    

提交回复
热议问题