gpt4 book ai didi

python - 按特定顺序连接点

转载 作者:行者123 更新时间:2023-11-30 22:04:28 25 4
gpt4 key购买 nike

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

我正在 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

剧情:

enter image description here

这会在 map 上绘制点,其想法是找到从起点到终点的最短路径。忘记执行此操作的算法,假设我得到的输出表示路线为:

[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])

关于python - 按特定顺序连接点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53277905/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com