在二维表中,XY轴可能会发生变化,导致Z轴需要重新查表,如下:
当X,Y轴发生变化时,可以在如下代码中提取插值后的Z值,注意新的X,Y如果超出原有的范围,需要检查Z值是否正确。
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from scipy.interpolate import RegularGridInterpolator as RGI
from scipy.interpolate import griddata
# 导入数据,X,Y,Z
ff = pd.read_excel(r"D:\Test\M24p4 360V efficiency.xlsx",
sheet_name="interp2d")
x = pd.DataFrame(ff.iloc[0, 1:]) # rpm
y = pd.DataFrame(ff.iloc[1:, 0]) # torque
z = ff.values[1:, 1:] # eff
X, Y = np.meshgrid(x, y)
XX, YY, ZZ = X.flatten(), Y.flatten(), z.flatten()
# 导入新的X,Y轴
x1 = pd.read_excel(r"D:\Test\M24p4 360V efficiency.xlsx",
sheet_name="Sheet4", usecols=[0])
y1 = pd.read_excel(r"D:\Test\M24p4 360V efficiency.xlsx",
sheet_name="Sheet4", usecols=[1])
X1, Y1 = np.meshgrid(x1, y1)
# 导出新的Z
Z1 = griddata((XX, YY), ZZ, (X1, Y1), method='linear')