I have a list of cities and the distance between each city.

A -> B: 3km
A -> C: 5km
A -> D: 1km
B -> C: 1km
B -> D: 2km
C -> D: 5km

I would like to draw a graph where all cities are shown as points while the distances between the points are as near at the provided distances as possible. I am thinking of an algorithm that draws the points in space and tries to iteratively move the objects closer to the distance.

How can I accomplice that in Python?

  • $\begingroup$ Consider multidimensional scaling if the distance matrix is square symmetric. For rectangular matrix (bipartite graph) consider multidimensional unfolding or correspondence analysis. $\endgroup$
    – ttnphns
    Mar 15, 2021 at 11:36

1 Answer 1


This is the classic example for when multi-dimensional scaling (MDS) is appropriate. MDS maps points, for which dissimilarities are given, to a Cartesian space of a chosen dimension (in your case, two dimensions). MDS is available in scikit-learn.

  • $\begingroup$ Thanks, works as charms! $\endgroup$
    – simsi
    Mar 15, 2021 at 11:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.