Warm tip: This article is reproduced from stackoverflow.com, please click
coordinates graph list matplotlib python

How to plot and connect points in order?

发布于 2020-08-04 03:09:22

I have a list of coordinates arranged in a specific order.

shortest_route = [(2, 8), (2, 8), (1, 3), (0, 2), (0, 0), (6, 1), (9, 3), (8, 4), (7, 4), (6, 4), (2, 8)]

I am trying to plot the coordinates points and connect them in that order. My idea was to iterate through the list using a for loop, then plotting the coordinate points one by one, and connecting them with a line.

for g in shortest_route:
    print(g)
    plt.plot(x, y, '-o')
plt.show()

enter image description here

Based on the image, I can tell that the points are not connected in order and that the shape of the graph is not closed off. The last two coordinate points line would allow the graph to be closed off.

Questioner
Ummaromana Sama
Viewed
3
Sheldore 2020-05-14 05:20

You can just unpack the list of tuples into x and y data using zip and do

x, y = zip(*shortest_route)

plt.plot(x, y, '-o')

enter image description here