0

What's the easiest way to format this array data so that I can connect the points on the graph iterating, assuming each line contains x and y coordinates of a single point?

import matplotlib.pyplot as plt
(...)

<class 'numpy.ndarray'>
[-22.58343371   7.97162262]
[-49.08400669 -28.64111278]
[-71.47754547 -25.78248676]
[-46.27120899 -21.72541444]
[ 43.6158669  109.61815799]
[-22.58343371   7.97162262]

(...)

plt.plot(x, y, color='orange')

Sorry, below is almost complete code (order is calculated by another alghoritm) corrected thanks to Quang Hoang's comment. This is obviously part of the solution to the well known tsp problem. The point, of course, is to connect the points according to the correct order in the list which in this case is: (0, 2, 1, 3, 4, 0).

from sklearn.decomposition import PCA
import matplotlib.pyplot as plt
import numpy as np

n = 5
order = (0, 2, 1, 3, 4, 0)

distances = [[0, 39, 22, 59, 54, 33, 57, 32, 89, 73, 29, 46],
             [39, 0, 20, 20, 81, 8, 49, 64, 63, 84, 10, 61],
             [22, 20, 0, 39, 74, 18, 60, 44, 71, 73, 11, 46],
             [59, 20, 39, 0, 93, 27, 51, 81, 48, 80, 30, 69],
             [54, 81, 74, 93, 0, 73, 43, 56, 104, 76, 76, 77],
             [33, 8, 18, 27, 73, 0, 45, 61, 71, 88, 8, 63],
             [57, 49, 60, 51, 43, 45, 0, 85, 88, 115, 52, 103],
             [32, 64, 44, 81, 56, 61, 85, 0, 74, 43, 55, 23],
             [89, 63, 71, 48, 104, 71, 88, 74, 0, 38, 69, 51],
             [73, 84, 73, 80, 76, 88, 115, 43, 38, 0, 81, 28],
             [29, 10, 11, 30, 76, 8, 52, 55, 69, 81, 0, 55],
             [46, 61, 46, 69, 77, 63, 103, 23, 51, 28, 55, 0]]

pca = PCA(n_components=2)
coord = pca.fit_transform(distances[:n])

plt.scatter(coord[:,0], coord[:,1])

for i in coord:
    x = np.where(coord == i)
    plt.annotate((x[0][0]) ,i, color='red')

for j in order:
    print(coord[j])
    plt.plot(coord[:,0], coord[:,1], color='orange')
plt.show()
2
  • Please post the full code. Also, what do you mean by "connect the points on the graph iterating"? Commented Dec 17, 2020 at 17:25
  • plt.plot(arr[:,0], arr[:,1])? Commented Dec 17, 2020 at 17:26

2 Answers 2

1

Sometimes, after many hours, things feel more difficult than they are:) Thank's a lot Kapocsi! Working solution below:

from sklearn.decomposition import PCA
import matplotlib.pyplot as plt
import numpy as np

n = 5
order = [0, 2, 1, 3, 4, 0]

distances = [[0, 39, 22, 59, 54, 33, 57, 32, 89, 73, 29, 46],
             [39, 0, 20, 20, 81, 8, 49, 64, 63, 84, 10, 61],
             [22, 20, 0, 39, 74, 18, 60, 44, 71, 73, 11, 46],
             [59, 20, 39, 0, 93, 27, 51, 81, 48, 80, 30, 69],
             [54, 81, 74, 93, 0, 73, 43, 56, 104, 76, 76, 77],
             [33, 8, 18, 27, 73, 0, 45, 61, 71, 88, 8, 63]]

pca = PCA(n_components=2)
coord = pca.fit_transform(distances[:n])

plt.scatter(coord[:,0], coord[:,1])

for i in coord:
    x = np.where(coord == i)
    plt.annotate((x[0][0]) ,i, color='red')

x = coord[order]
print(x)

plt.plot(x[:, 0], x[:, 1], color='orange', marker='o', linestyle='-')
plt.show()
Sign up to request clarification or add additional context in comments.

Comments

0

You likely need to sort the data to get the visualization you want:

import numpy as np
import matplotlib.pyplot as plt


a = np.array([[-22.58343371,   7.97162262],
              [-49.08400669, -28.64111278],
              [-71.47754547, -25.78248676],
              [-46.27120899, -21.72541444],
              [ 43.6158669 , 109.61815799],
              [-22.58343371,   7.97162262]])


order = a[:, 0].argsort()
plt.plot(a[order, 0], a[order, 1], color='orange', marker='o', linestyle='-')
plt.show()

Figure_1

If you don't sort it, you will end up with something like this:

import numpy as np
import matplotlib.pyplot as plt


a = np.array([[-22.58343371,   7.97162262],
              [-49.08400669, -28.64111278],
              [-71.47754547, -25.78248676],
              [-46.27120899, -21.72541444],
              [ 43.6158669 , 109.61815799],
              [-22.58343371,   7.97162262]])

plt.plot(a[:, 0], a[:, 1], color='orange', marker='o', linestyle='-')
plt.show()

Figure_2

1 Comment

Thank you Kapocsi, but I would like to plot lines in given order.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.