【问题标题】:How to plot routes in python如何在python中绘制路线
【发布时间】:2022-01-22 00:56:37
【问题描述】:

我最近一直在研究旅行商问题。我需要在坐标系上获得路线可视化。我有经度和纬度作为系列: [x_c],[y_c]

我得到了TSP解决方案,根据解决方案我最好的路线是:

best_route2
array([ 0.,  7., 10.,  8.,  9.,  1., 13., 11.,  5.,  6., 12.,  4.,  3.,
    2.,  0.])

我开始绘制如下。但我找不到以最佳路线连接点的方法。

plt.plot(x_c[0], y_c[0], c='r', marker='*')
plt.scatter(x_c[1:], y_c[1:], c='b')

plot

如何按以下顺序连接点? 0 - 7 - 10 - 8 - 9 - 1 - 13 - 11 - 5 - 6 - 12 - 4 - 3 - 2 - 0

谢谢!

【问题讨论】:

    标签: python plot coordinate-systems


    【解决方案1】:

    这是一个代码 sn-p,您可以修改以满足您的要求:

    import numpy as np
    import matplotlib.pyplot as plt
    
    #number of points
    n_points = 8
    
    #for the reproducibility purpose
    np.random.seed(12345)
    
    #an array to be used as the hypothetical order
    myorder=np.random.choice(range(n_points), n_points, replace=False)
    
    #sample coordinates
    x_c = np.arange(n_points)
    y_c = np.random.uniform(0,n_points,n_points)
    
    #sorting coordinates
    x_c_sorted=[]
    y_c_sorted=[]
    for sn in myorder:
        x_c_sorted.append(x_c[sn])
        y_c_sorted.append(y_c[sn])
        
    #plotting
    plt.plot(x_c_sorted, y_c_sorted, '--o')
    plt.show()
    

    【讨论】:

      猜你喜欢
      • 2017-02-13
      • 1970-01-01
      • 1970-01-01
      • 2014-09-20
      • 1970-01-01
      • 2018-01-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多