arr_x我有两个 Float64:和数组arr_y。我有一个 2D 数组,arr_z存储一些在点集 (x,y) 上使用xin arr_x、yin计算的函数 z(x,y) arr_y。
如何arr_z使用arr_x和进行二维插值arr_y?也就是说,我想要一个函数itp_z(x,y),它相当于线性插值,但现在在数组 arr_z 上以二维形式进行插值。
Interpolations.jl应该适用于此:
using Interpolations, Plots
# Data
x = range(-2, 3, length=20)
y = range(3, 4, length=10)
z = @. cos(x) + sin(y')
# Interpolation object (caches coefficients and such)
itp = LinearInterpolation((x, y), z)
# Fine grid
x2 = range(extrema(x)..., length=300)
y2 = range(extrema(y)..., length=200)
# Interpolate
z2 = [itp(x,y) for y in y2, x in x2]
# Plot
p = heatmap(x2, y2, z2, clim=(-2,2), title="Interpolated heatmap")
scatter!(p, [x for _ in y for x in x], [y for y in y for _ in x], zcolor=z[:]; lab="original data", clim=(-2,2))
Run Code Online (Sandbox Code Playgroud)