如何在二维非规则网格数据中插入切片?

问题描述 投票:0回答:1

我想在二维网格数据(非常规)中插入一个切片(蓝线)。

最小示例。我想在蓝线中插入 psi 的数据 -

Minimal Example. I want to interpolate the data of psi in that blue line

import numpy as np
import matplotlib.pyplot as plt


x = np.linspace(-50,50,100)
y = np.linspace(400,500,100)

X, Y = np.meshgrid(x,y)


Vec = np.array([X, Y])

psi = 90 - np.rad2deg(np.arccos(Vec[0,:,:] / np.linalg.norm(Vec, axis=0)))

R = np.sqrt(X ** 2 + Y ** 2)

# points for interpolation
x_points = np.linspace(x.min(), x.max())
y_points = [480] * len(x_points)


fig, ax = plt.subplots()
ax.contourf(X, R, psi, level=20)
ax.plot(x_points, y_points)

我发现的 scipy 函数的问题是,它们要么无法读取非规则网格数据,要么只是在新网格上进行插值 -> 昂贵。

在这个最小的例子中是否有一个函数只插入几个点(x_points 和 y_points)?

python numpy scipy interpolation
1个回答
0
投票

这不是一个插值问题。您的函数是已知的,因此只需使用您显示的固定 y 值来调用它即可:

import numpy as np
import matplotlib.pyplot as plt


x = np.linspace(-50,50, num=50)
y = 480
r = np.sqrt(y**2 + x**2)
psi = 90 - np.rad2deg(np.arccos(x / r))

fig, ax = plt.subplots()
ax.plot(x, psi)
ax.set_xlabel('x')
ax.set_ylabel('psi')
ax.set_title(f'y={y}')
plt.show()
© www.soinside.com 2019 - 2024. All rights reserved.