找到从(x,y)坐标

时间:2017-10-26 15:21:45

标签: python scipy numerical-methods numerical-integration

我目前有一个python脚本,它读入一个3列文本文件,其中包含助行器的x和y坐标以及它们行走的时间。

我已阅读此数据并将其分配到numpy数组中,如下面的代码所示:

import numpy as np
import matplotlib.pyplot as plt

data = np.loadtxt("info.txt", delimiter = ',')

x = data[:,0]
y = data[:,1]
t = data[:,2]

文件格式如下(x,y,t):

5907364.2371    -447070.881709  2193094
5907338.306978  -447058.019176  2193116
5907317.260891  -447042.192668  2193130

我现在想要找到步行者行进的距离作为时间的函数。我能想到的一种方法是将x坐标的差异和循环中y坐标的所有差异相加。这似乎是一个非常漫长的方法,但我认为它可以用一种数值积分来解决。有没有人对我能做什么有任何想法?

3 个答案:

答案 0 :(得分:3)

要计算距离"沿途",您必须首先获得每一步的距离。

这可以通过索引dx = x[1:]-x[:-1]在组件方面获得。然后,每步的距离为" dx的平方根** 2 + dy ** 2"请注意,此数组的长度小于1,因为相对于步数的间隔减少一个。这可以通过分配距离" 0"到了第一次数据。这是" concatenate"的作用。下面的一行。

这里没有数字积分,而是累积和。要执行数值积分,您需要运动方程(例如)。

额外更改:我使用np.loadtxt unpack=True参数来保存几行。

import numpy as np
import matplotlib.pyplot as plt

x, y, t = np.loadtxt("info.txt", unpack=True)

dx = x[1:]-x[:-1]
dy = y[1:]-y[:-1]

step_size = np.sqrt(dx**2+dy**2)

cumulative_distance = np.concatenate(([0], np.cumsum(step_size)))

plt.plot(t, cumulative_distance)

plt.show()

答案 1 :(得分:0)

通常,为了让步行距离更远,您可以总结较小的距离。你的步行者可能不是在网格上行走(也就是说,x中的一步和y中的一步),而是对角线(想想毕达哥拉斯定理)

所以,在python中它可能看起来像这样......

distanceWalked = 0
for x_y_point in listOfPoints:
    distanceWalked = distanceWalked  + (x_y_point[0] **2 + x_y_point[1] **2)**.5

其中listOfPoints类似于[[0,0],[0,1],[0,2],[1,2],[2,2]]

或者,您可以使用pandas。

import pandas as pd
df = pd.read_csv('info.txt',sep = '\t')
df['helpercol'] = (df['x']**2 +df['y']**2 )**.5
df['cumDist'] = df['helpercol'].cumsum()

现在,您的数据框中每次累积距离

答案 2 :(得分:0)

有几种方法可以获得点之间的欧几里德距离:

Numpy:

import numpy as np
dist = np.linalg.norm(x-y)
dist1= np.sqrt(np.sum((x-y)**2)))

SciPy的:

from scipy.spatial import distance
dist = distance.euclidean(x,y)
相关问题