python - 从曲线形状中获取均匀分布的点

标签 python numpy matplotlib scipy curve

我如何获取在其曲线上有更多点创建的形状并将其分割,以便这些点沿曲线更均匀地分布?在我的研究中,我认为 numpyinterp可能是正确的功能,但我不知道参数使用什么 (x, xp, fp, left, right, & period).任何帮助将不胜感激!

这是一个显示所需输出的动画。

Example of Shape with Even Distribution

这是输入圆角矩形的代码:

from matplotlib import pyplot as plt
import numpy as np

x_values = [1321.4, 598.6, 580.6, 563.8, 548.6, 535.4, 524.5, 516.2, 511,
509.2, 509.2, 511, 516.2, 524.5, 535.4, 548.6, 563.8, 580.6, 598.6, 1321.4, 1339.4,
1356.2, 1371.4, 1384.6, 1395.5, 1403.8, 1409, 1410.8, 1410.8, 1409, 1403.8, 1395.5,
1384.6, 1371.4, 1356.2, 1339.4, 1321.4]
y_values = [805.4, 805.4, 803.5, 798.3, 790.1,
779.2, 766, 750.8, 734, 716, 364, 346, 329.2, 314, 300.8, 289.9, 281.7, 276.5, 274.6,
274.6, 276.5, 281.7, 289.9, 300.8, 314, 329.2, 346, 364, 716, 734, 750.8, 766, 779.2,
790.1, 798.3, 803.5, 805.4]

fig, ax = plt.subplots(1)
ax.plot(x_values,y_values)
ax.scatter(x_values,y_values)
ax.set_aspect('equal')
plt.show()

谢谢!

最佳答案

enter image description here

from matplotlib import pyplot as plt
import numpy as np

x = np.array([1321.4, 598.6, 580.6, 563.8, 548.6, 535.4, 524.5, 516.2, 511,
509.2, 509.2, 511, 516.2, 524.5, 535.4, 548.6, 563.8, 580.6, 598.6, 1321.4, 1339.4,
1356.2, 1371.4, 1384.6, 1395.5, 1403.8, 1409, 1410.8, 1410.8, 1409, 1403.8, 1395.5,
1384.6, 1371.4, 1356.2, 1339.4, 1321.4])
y = np.array([805.4, 805.4, 803.5, 798.3, 790.1,
779.2, 766, 750.8, 734, 716, 364, 346, 329.2, 314, 300.8, 289.9, 281.7, 276.5, 274.6,
274.6, 276.5, 281.7, 289.9, 300.8, 314, 329.2, 346, 364, 716, 734, 750.8, 766, 779.2,
790.1, 798.3, 803.5, 805.4])

fig, ax = plt.subplots(1)
ax.set_aspect('equal')
ax.scatter(x, y, s=40, zorder=3, alpha=0.3)

# compute the distances, ds, between points
dx, dy = x[+1:]-x[:-1],  y[+1:]-y[:-1]
ds = np.array((0, *np.sqrt(dx*dx+dy*dy)))

# compute the total distance from the 1st point, measured on the curve
s = np.cumsum(ds)

# interpolate using 200 point
xinter = np.interp(np.linspace(0,s[-1], 200), s, x)
yinter = np.interp(np.linspace(0,s[-1], 200), s, y)

# plot the interpolated points
ax.scatter(xinter, yinter, s=5, zorder=4)
plt.show()

关于python - 从曲线形状中获取均匀分布的点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/75416188/

相关文章:

python - python列表中的索引错误

python - django 管理错误 - 'django_content_type.name' 中的未知列 'field list'

python - 选择所有 float 类型的所有列

python - 无法保存matplotlib.figure图,canvas为None

python - 在 matlibplot python 中以对数刻度填充曲线下的区域

python - 如何使用 Fabric 处理部署到具有多个应用程序服务器的单个主机?

python - Sklearn SVM - 如何获取错误预测列表?

python - 高效地将 CSV 的最后 'n' 行读入 DataFrame

python - 为什么 apply(type) 在 pandas 中得到不一致的结果?

python - 显示直方图条形轮廓