0

I have the following lists that correspond to 6 (clients) points (each one having an id, and x and y coordinates)

allIds = [0, 1, 2, 3, 4, 5], 
allxs = [50, 25, 43, 80, 25, 18]
allys = [50, 54, 96, 50, 90, 47]

For example, the 1st point has an id of 0, its x coordinates are 50, and its y coordinates are 50, and so on.

I am trying to solve a traveling salesman problem, so I want to plot the points of a specific route between points, connected with a line that will represent the closed route.

The final route I want to plot is represented by the following list:

final_route = [0, 4, 5, 2, 1, 3, 0]

and represents a path between clients' ids

So far i have only managed to plot the points only, with the following code:

fig, ax = plt.subplots()
fig.set_size_inches(6, 6)
ax.plot(all_x, all_y, ls="", marker="o", markersize=8)
for xi, yi, pidi in zip(all_x, all_y, all_ids):
    ax.annotate(str(pidi), xy=(xi,yi))

plt.xlim([0, 100])
plt.ylim([0, 100])
plt.show()

Which produces the following plot: Plot of poits

Any ideas about how to plot the line between the points? Thanks

Yannis_Tr
  • 15
  • 5
  • @BigBen As far as i am concerned, there is an option to plot a path by changing `marker="o"` but still i cant figure out how would that work.. – Yannis_Tr Oct 18 '22 at 16:41
  • If you'd convert your lists to numpy arrays, you'd be able to index `allxs` and `allys` by `final_route`. So, `allxs = np.array(allxs); allys = np.array(allys); final_route = np.array(final_route); ax.plot(allxs[final_route], allys[final_route])` – JohanC Oct 18 '22 at 17:09
  • @JohanC Thanks..but `allxs[final_route]` returns `[50 50 50 50 50 50 50]` – Yannis_Tr Oct 18 '22 at 17:23
  • Well, if I try this with the given example, I get `array([50, 25, 18, 43, 25, 80, 50])` – JohanC Oct 18 '22 at 17:26
  • @JohanC It seems you are right..my code had a logical error and i didn't notice..this implementation works though but points can not be seen, unfortunately..Many thanks though – Yannis_Tr Oct 18 '22 at 17:37
  • Doesn't `ax.plot(all_x, all_y, ls="", marker="o", markersize=8)` show the points? You can plot both onto the same `ax`. Or you could draw both in one go `ax.plot(allxs[final_route], allys[final_route], ls="-", marker="o", markersize=8)` – JohanC Oct 18 '22 at 17:52

0 Answers0