Home > Mobile >  How to plot the lists from a dictionary that correspond to indices of points with x and y coordinate
How to plot the lists from a dictionary that correspond to indices of points with x and y coordinate

Time:10-30

Say i have a dictionary named "DictionaryOfRoutes" that includes lists like in the following example:

'DictionaryOfRoutes'= {'RouteOfVehicle_1': [0, 6, 1, 5, 0],
                       'RouteOfVehicle_2': [0, 4, 3, 0],
                       'RouteOfVehicle_3': [0, 2, 0]
}

The lists in the values of it correspond to routes and the integers inside them to indices of points. For the points i have the following data in the form of lists:

allIds = [0, 1, 2, 3, 4, 5, 6] 
allxs = [50, 18, 33, 98, 84, 13, 50]
allys = [50, 73, 16, 58, 49, 63, 56]

For example the first point has an id of 0, x coordinates of 50, and y coordinates of 50, and so on.. I want to plot these routes like in the following png: enter image description here

CodePudding user response:

I hope this helps, usings the keys() function on the dictionary object for a loop helps.

final_routes=[]
for key in DictionaryOfRoutes.keys(): 
         temp=[]
         for index in DictionaryOfRoutes[key]:
              temp.append(all_Ids[index])
         final_routes.append(temp)         
SolutionPlot(xx,yy,all_Ids,final_routes)

and in the function I added this,

for final_route in final_routes:
        final_route = np.array(final_route) 
        
        ax.plot(allxs[final_route], allys[final_route], ls="-", marker="x", markersize=6)
        plt.xlim([0, 100])
        plt.ylim([0, 100])
        plt.title("Travelling Salesman (Nearest Neighbor Algorithm)")

        for xi, yi, pidi in zip(xx, yy, all_ids):
            ax.annotate(str(pidi), xy=(xi,yi))
        plt.show()

I am not so sure what the matrix variable is for. I started this expecting to have to use plt.hold(True) but this wasn't the case, and I'm not sure why.

  • Related