我想在二维网格数据(非常规)中插入一个切片(蓝线)。
最小示例。我想在蓝线中插入 psi 的数据 -
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)?
这不是一个插值问题。您的函数是已知的,因此只需使用您显示的固定 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()