Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/318.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python:沿连接点集的直线等距点_Python_Python 3.x_Numpy_Matplotlib_Scipy - Fatal编程技术网

Python:沿连接点集的直线等距点

Python:沿连接点集的直线等距点,python,python-3.x,numpy,matplotlib,scipy,Python,Python 3.x,Numpy,Matplotlib,Scipy,作为numpy.linspace,给出线性连接的两点之间的等距点。我可以得到连接一组点的直线上的等距点吗 例如: import numpy as np npts = 10 xcoords, ycoords = [0,1], [0,1] xquery = np.linspace(xcoords[0],xcoords[1], npts) yquery = np.linspace(ycoords[0],ycoords[1], npts) 这里,我需要在连接一组点的线上等距的查询点 xcoords,

作为numpy.linspace,给出线性连接的两点之间的等距点。我可以得到连接一组点的直线上的等距点吗

例如:

import numpy as np
npts = 10
xcoords, ycoords = [0,1], [0,1]
xquery = np.linspace(xcoords[0],xcoords[1], npts)
yquery = np.linspace(ycoords[0],ycoords[1], npts)
这里,我需要在连接一组点的线上等距的查询点

xcoords, ycoords = [0,1,5,8], [0,3,6,7]

编辑:澄清此答案仅提供x方向上的等距点。我对这个问题的误解

我相信你要找的是插值? scipy.interpolate的文档如下:

但要快速显示您的示例:

from scipy.interpolate import interp1d
x=[0,1,5,8]
y=[0,3,6,7]
f=interp1d(x,y)
然后只需输入您希望查询到f中的新x点,就像这样(xnew不能超过x的最小/最大界限)

看一看,这只是一个情节

import matplotlib.pyplot as plt
plt.plot(xnew,ynew,'ro',x,y,'x')
plt.show()

编辑:澄清此答案仅提供x方向上的等距点。我对这个问题的误解

我相信你要找的是插值? scipy.interpolate的文档如下:

但要快速显示您的示例:

from scipy.interpolate import interp1d
x=[0,1,5,8]
y=[0,3,6,7]
f=interp1d(x,y)
然后只需输入您希望查询到f中的新x点,就像这样(xnew不能超过x的最小/最大界限)

看一看,这只是一个情节

import matplotlib.pyplot as plt
plt.plot(xnew,ynew,'ro',x,y,'x')
plt.show()

按等长部分细分二维分段线:

import matplotlib.pyplot as plt
%matplotlib inline

from scipy.interpolate import interp1d
import numpy as np


x = [0, 1, 8, 2, 2]
y = [1, 0, 6, 7, 2]

# Linear length on the line
distance = np.cumsum(np.sqrt( np.ediff1d(x, to_begin=0)**2 + np.ediff1d(y, to_begin=0)**2 ))
distance = distance/distance[-1]

fx, fy = interp1d( distance, x ), interp1d( distance, y )

alpha = np.linspace(0, 1, 15)
x_regular, y_regular = fx(alpha), fy(alpha)

plt.plot(x, y, 'o-');
plt.plot(x_regular, y_regular, 'or');
plt.axis('equal');

将二维分段线细分为等长部分:

import matplotlib.pyplot as plt
%matplotlib inline

from scipy.interpolate import interp1d
import numpy as np


x = [0, 1, 8, 2, 2]
y = [1, 0, 6, 7, 2]

# Linear length on the line
distance = np.cumsum(np.sqrt( np.ediff1d(x, to_begin=0)**2 + np.ediff1d(y, to_begin=0)**2 ))
distance = distance/distance[-1]

fx, fy = interp1d( distance, x ), interp1d( distance, y )

alpha = np.linspace(0, 1, 15)
x_regular, y_regular = fx(alpha), fy(alpha)

plt.plot(x, y, 'o-');
plt.plot(x_regular, y_regular, 'or');
plt.axis('equal');

那么,在指定的2个4D点之间需要等距离的4D点吗?像网格一样?如果不清楚,很抱歉。它们只是二维点(0,0)、(1,3)、(5,6)和(8,7)。我想沿着连接上述2D点的直线得到等距点。类似于使用QGIS here()完成的解决方案。您正在寻找一个已实现的解决方案(我不知道任何解决方案),或者您希望自己实现它?因此,您需要在指定的2个4D点之间等距4D点?像网格一样?如果不清楚,很抱歉。它们只是二维点(0,0)、(1,3)、(5,6)和(8,7)。我想沿着连接上述2D点的直线得到等距点。与使用QGIS here()完成的类似。您正在寻找一个已实现的解决方案(我不知道是否有),或者您想自己实现它?是的,我们可以进行插值,但它不会沿直线提供等距点。从你的解来看,前两个红色圆点之间的距离比后两个红色圆点之间的距离大。哦,我明白了。是的,我做的插值在x上是等距的,但不是yYes,我们可以做插值,但它不能给出沿直线的等距点。从你的解来看,前两个红色圆点之间的距离比后两个红色圆点之间的距离大。哦,我明白了。是的,我做的插值在x上是等距的,但在xdze2上不是很像你。你的回答解决了我的问题,对我帮助很大。非常感谢@xdze2。你的回答解决了我的问题,对我帮助很大。