python - python scipy 中样条线与平面的相交

标签 python numpy scipy

我有一堆 3D 数据点,我使用 scipy 薄板样条线通过它们拟合表面,如下所示:

import numpy as np
import scipy as sp
import scipy.interpolate

# x, y, z are the 3D point coordinates

spline = sp.interpolate.Rbf(x, y, z, function='thin_plate', smooth=5, episilon=5)
x_grid = np.linspace(0, 512, 1024)
y_grid = np.linspace(0, 512, 1024)
B1, B2 = np.meshgrid(x_grid, y_grid, indexing='xy')
Z = spline(B1, B2)

这适合所需的表面,如附图所示。

enter image description here

现在我想做的是能够查询该样条线与给定平面的相交位置。

因此,给定这个拟合曲面,我如何查询该曲面与平面 (z = 25) 相交的 (x, y) 点。

所以,上面的代码是合适的:

z = f(x, y)

现在已经安装了f,我想知道是否可以进行反向查找,即我想做f^{-1}(z)

最佳答案

3D 等高线图可以很好地将等高线插值到所需的高度:

from matplotlib import pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
import scipy as sp
import scipy.interpolate

N = 10
x = np.random.uniform(100, 400, N)
y = np.random.uniform(100, 400, N)
z = np.random.uniform(0, 100, N)
# x, y, z are the 3D point coordinates
spline = sp.interpolate.Rbf(x, y, z, function='thin_plate', smooth=5, episilon=5)
x_grid = np.linspace(0, 512, 1024)
y_grid = np.linspace(0, 512, 1024)
B1, B2 = np.meshgrid(x_grid, y_grid, indexing='xy')
Z = spline(B1, B2)

fig = plt.figure()
ax = fig.add_subplot(111, projection="3d")
ax.contour(B1, B2, Z, levels=[25], offset=25, colors=['red'])
ax.plot_surface(B1, B2, Z, cmap='autumn_r', lw=1, rstride=10, cstride=10, alpha=0.5)

plt.show()

contour

PS:如果您需要曲线的 xy 坐标,它们将作为 2d 坐标列表的列表存储在轮廓内

contour = ax.contour(B1, B2, Z, levels=[25], offset=25, colors=['red'])
for segments in contour.allsegs:
    for segment in segments:
        print("X:", segment[:,0])
        print("Y:", segment[:,1])

关于python - python scipy 中样条线与平面的相交,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63261363/

相关文章:

python - 为什么 NumPy 有时比 NumPy + 普通 Python 循环慢?

python - 获取列表之间第一个和最后一个公共(public)元素索引的最快方法

python-2.7 - 导入 scipy.stats 时出现 ImportError, undefined symbol (scipy 版本 0.14.0)

python - Scipy:对数正态拟合

python - 找出要删除的内容

Python:无法打开文件进行读取

python - 如何矢量化 Fisher 精确检验?

python - 将特定线条添加到 Plotly Scatter3d() 图中

python - Scipy signal.find_peaks_cwt 挂断

python - 使用 scipy 的 solve_bvp 求解 BVP