0

I have a distance matrix:

dis_matrix = torch.tensor([[ 0.0000, 20.2615, 20.6478, 21.8596, 20.5779],
        [20.2615,  0.0000, 21.4059, 22.5908, 21.6969],
        [20.6478, 21.4059,  0.0000, 21.3755, 21.4672],
        [21.8596, 22.5908, 21.3755,  0.0000, 24.2236],
        [20.5779, 21.6969, 21.4672, 24.2236,  0.0000]])

Where the names are:

names = ['1', '2', '3', '4', '5']

How can I visualize it in 2D in matplotlib? That is, plotting the name of each variable on a 2D plot that will be x distance away from the other values (where x is clearly the distances from the distance matrix).

There are many answers using networkx (like here), or using MDS (like here), but I just want to create a simple 2D plot using the distances.

Penguin
  • 1,923
  • 3
  • 21
  • 51
  • 1
    are you sure that this can be visualised in 2d. Consider three items A,B,C. A is close to B, but very far from C, but C is very close to B. This is perfectly fine in a distance matrix, but cant be shown in 2D. – Christian Sloper Jul 06 '21 at 13:46
  • 1
    Why don't you like [NetworkX](https://networkx.org/)? I would quess that this is the simplest way for a 2D plot – max Jul 06 '21 at 14:51
  • 1
    @ChristianSloper It's even worse than that. In fact you can have a valid finite metric space as in this question (where the triangle inequality holds) but that's still no guarantee that a (isometric) planar embedding exists. – jodag Jul 06 '21 at 15:55
  • Hmm I see your point guys. @max the reason I don't want to use networkx is because I don't care for the edges. I just need the plot to have the points with their names on it – Penguin Jul 06 '21 at 16:14

0 Answers0