3

I'm tyring to use Networkx to visualize a distance matrix. What I want is a graph where the edge length between nodes is proportional to the distance between them in the distance matrix. I wrote the following code but it does not look like the resulting network takes the distance matrix into account at all. For instance, there are a few nodes that have distance of zero between them and they are not close at all in the resulting graph. How do I make this work?

names,data=loadDataSet()
#get distance matrix
dist_mat=cdist(data,data)

G=nx.from_numpy_matrix(dist_mat)

mapping=dict(zip(G.nodes(),names))
G = nx.relabel_nodes(G,mapping)

nx.draw(G)
pl.show()
user1893354
  • 5,778
  • 12
  • 46
  • 83
  • 1
    Maybe some of the advice in the answer here http://stackoverflow.com/questions/13513455/drawing-a-graph-or-a-network-from-a-distance-matrix is useful? – Aric Jun 06 '13 at 11:47
  • Yes I saw that post and I tried to emulate the code, but perhaps it is not possible – user1893354 Jun 12 '13 at 20:44
  • Does this answer your question? [Drawing a graph or a network from a distance matrix?](https://stackoverflow.com/questions/13513455/drawing-a-graph-or-a-network-from-a-distance-matrix) – SultanOrazbayev Dec 21 '21 at 16:18

0 Answers0