Python 按特定顺序连接点

Python 按特定顺序连接点,python,python-3.x,matplotlib,Python,Python 3.x,Matplotlib,我将要描述的完整代码(不包括路径查找算法)可以在上找到 我正在用Python从一个文本文件中读取10个坐标。然后,我继续将纬度和经度坐标传递给一个函数,该函数按如下方式打印这些点 def read_two_column_file(file_name): with open(file_name, 'r') as f_input: csv_input = csv.reader(f_input, delimiter=' ', skipinitialspace=True, )

我将要描述的完整代码(不包括路径查找算法)可以在上找到

我正在用Python从一个文本文件中读取10个坐标。然后,我继续将纬度和经度坐标传递给一个函数,该函数按如下方式打印这些点

def read_two_column_file(file_name):
    with open(file_name, 'r') as f_input:
        csv_input = csv.reader(f_input, delimiter=' ', skipinitialspace=True, )
        long = []
        lat = []
        for col in csv_input:
            x = float(col[0])  # converting to float
            y = float(col[1])
            long.append(x)
            lat.append(y)

    return long, lat


def display_points(long, lat):
    plt.figure()
    plt.gca().set_aspect('equal', adjustable='box')
    plt.ylabel('latitude')
    plt.xlabel('longitude')
    plt.title('longitude vs latitude')
    plt.scatter(lat, long)
    plt.orientation = u'vertical'
    plt.grid('True')
    plt.show()
样本输入:

35.905333, 14.471970
35.896389, 14.477780
35.901281, 14.518173
35.860491, 14.572245
35.807607, 14.535320
35.832267, 14.455894
35.882414, 14.373217
35.983794, 14.336096
35.974463, 14.351006
35.930951, 14.401137
绘图:

这将在地图上绘制点,目的是找到从起点到终点的最短路线。忘记了这样做的算法,让我们假设我得到一个表示路由的输出,如下所示:

[2, 1, 0, 9, 8, 7, 6, 5, 4, 3, 2]

如何将这些节点转换回它们所表示的坐标,以便将它们连接到Matplotlib上?

将纬度和经度转换为numpy数组:

long = np.array(long)
lat = np.array(lat)
我建议直接在
read_two_column_file
中执行此操作

然后,如果路径在变量
path
中,则可以直接执行以下操作:

plt.plot(long[path], lat[path])