gpt4 book ai didi

python - 从python中的图形中提取点

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:52:42 25 4
gpt4 key购买 nike

我一直在寻找一种方法来从我用 Python 绘制的 4 个图中提取点,以便在这些点上运行 Ramer Douglas peucker 算法。理想情况下,数据将以一系列 x 和 y 坐标呈现,因为当前数据集呈现方式不同。每个图表由大约 2000 个点组成,但它们没有格式化为数据集中的 X 和 Y 坐标。如果有人有任何建议,我附上了用于绘制图表和数据集的代码。

干杯!

最佳答案

插入代码行 plt.gca().lines[-1].get_xydata()(就在您创建绘图的行下方:plt.plot(distances, alts, color='blue')) 将允许您为绘制的每条线的所有点提取 x-y 坐标。

然后我创建了一个空列表 line_coords 来存储四行中每一行的 x-y 坐标,然后使用 RDP 算法迭代这四组坐标中的每一组坐标:

rdp_res = []
for line in line_coords:
rdp_res.append(rdp(line))

列表 rdp_res 包含四行中每一行的 RDP 算法输出:

[array([[4.92025194e+00, 3.80000000e+01],
[5.24522347e+01, 3.80000000e+01],
[8.59726863e+01, 3.77000000e+01],
...,
[3.07740662e+04, 3.60000000e+01],
[3.08035662e+04, 3.60000000e+01],
[3.08075068e+04, 3.59000000e+01]]),
array([[4.92025194e+00, 3.80000000e+01],
[5.24522347e+01, 3.80000000e+01],
[8.59726863e+01, 3.77000000e+01],
...,
[2.81487733e+04, 5.20000000e+01],
[2.81536662e+04, 5.18000000e+01],
[2.82000946e+04, 5.18000000e+01]]),
array([[4.92025194e+00, 3.80000000e+01],
[5.24522347e+01, 3.80000000e+01],
[8.59726863e+01, 3.77000000e+01],
...,
[2.37758154e+04, 1.26000000e+01],
[2.37973123e+04, 1.30000000e+01],
[2.38301772e+04, 1.38000000e+01]]),
array([[4.92025194e+00, 3.80000000e+01],
[5.24522347e+01, 3.80000000e+01],
[8.59726863e+01, 3.77000000e+01],
...,
[2.59717233e+04, 1.83600000e+02],
[2.60321544e+04, 1.83400000e+02],
[2.60884831e+04, 1.83400000e+02]])]

我们可以比较每一行的坐标个数:

line_coords[0].shape, line_coords[1].shape, line_coords[2].shape, line_coords[3].shape

((1167, 2), (2133, 2), (2869, 2), (3597, 2))

每行运行 RDP 后剩余的坐标数:

rdp_res[0].shape, rdp_res[1].shape, rdp_res[2].shape, rdp_res[3].shape

((1080, 2), (1947, 2), (2643, 2), (3360, 2))

下面我粘贴了您的原始代码,其中包括我的所有修改:

"""
File for importing route data from a json file
"""

import json
import os
import matplotlib.pyplot as plt
from rdp import rdp
import numpy as np

def get_data(file_name):
"""
method to retrieve JSON data from "file"
:param file_name: string representing file in which JSON data is stored
:return data: Pyhonic data created from JSON file information
"""
with open(os.path.join(os.sys.path[0], file_name), "r") as data_file:
data = json.load(data_file) # load data from JSON file
return data

if __name__== "__main__":
file_name = 'json_data.json'
routes = get_data(file_name)

print("Total Time")
print(routes[0]["totalTime"])
print("Total Distance")
print(routes[0]["totalDistance"])

routesPos = 0
edgePos = 0
edgeDistance = 0
alts = []
distances = []
line_coords = []

while routesPos < len(routes):
while edgePos < len(routes[routesPos]["edges"]):
edgeDistance = edgeDistance + routes[routesPos]["edges"][edgePos]["edgeDistance"]
distances.append(edgeDistance)
alts.append(routes[routesPos]["edges"][edgePos]["endLocation"]["alt"])
edgePos += 1
plt.plot(distances, alts, color='blue')
coords = plt.gca().lines[-1].get_xydata() # Get coords for most recently plotted line
line_coords.append(coords)
edgeDistance = 0
routesPos += 1
edgePos = 0


rdp_res = []
for line in line_coords:
rdp_res.append(rdp(line))

关于python - 从python中的图形中提取点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54636178/

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