我正在使用 Pandas 和 Bokeh/Matplotlib 进行数据分析 我在数据框中有 X、Y 和 Z 列数据。我想用它创建一个曲面图(具有特定的网格点)(最好是三次/样条插值),看起来像下面这样。我更喜欢使用 Bokeh,但如果这是唯一的解决方案,我也可以使用 Matplotlib。
但我的主要问题是:从这个表面如何对 Xi 和 Yi 数组进行插值以获得相应的 Zi 数组,如下所示:
我已经尝试过 scipy 中的“griddata”,但无法按照我想要的插值方式进行操作。
您可以使用
matplotlib.plot_surface
函数进行绘图(https://matplotlib.org/stable/api/_as_gen/mpl_toolkits.mplot3d.axes3d.Axes3D.plot_surface.html)
你可以使用
scipy.interpolate.SmoothBivariateSpline
(https://docs.scipy.org/doc/scipy/reference/ generated/scipy.interpolate.SmoothBivariateSpline.html)有效插值:
from scipy.interpolate import SmoothBivariateSpline
x = np.array([1, 2, 3, 4, 5, 6, 7, 8] * 8)
y = np.array(sorted([1, 2, 3, 4, 5, 6, 7, 8] * 8))
z = np.array(
[i * j for i in [1, 2, 3, 4, 5, 6, 7, 8]
for j in [1, 2, 3, 4, 5, 6, 7, 8]])
spline = SmoothBivariateSpline(x, y, z)
print(spline(3, 3.5))
print(spline(1.5, 8))
Output:
[[10.5]]
[[12.]]