Skip to main content
 首页 » 编程设计

python之在具有负斜率的曲线上插值数据点

2025年05月04日8lhb25

您好,我有两个 numpy 数组(在本例中代表深度和百分比深度剂量数据),如下所示:

depth = np.array([ 0. ,  0.1,  0.2,  0.3,  0.4,  0.5,  0.6,  0.7,  0.8,  0.9,  1. , 
                   1.1,  1.2,  1.3,  1.4,  1.5,  1.6,  1.7,  1.8,  1.9,  2. ,  2.2, 
                   2.4,  2.6,  2.8,  3. ,  3.5,  4. ,  4.5,  5. ,  5.5]) 
pdd = np.array([  80.40649399,   80.35692155,   81.94323956,   83.78981286, 
                  85.58681373,   87.47056637,   89.39149833,   91.33721651, 
                  93.35729334,   95.25343909,   97.06283306,   98.53761309, 
                  99.56624117,  100.        ,   99.62820672,   98.47564754, 
                  96.33163961,   93.12182427,   89.0940637 ,   83.82699219, 
                  77.75436857,   63.15528566,   46.62287768,   29.9665386 , 
                  16.11104226,    6.92774817,    0.69401413,    0.58247614, 
                   0.55768992,    0.53290371,    0.5205106 ]) 

绘制时给出以下曲线:

我需要找到 pdd 下降到给定值(最初为 50%)的深度。我尝试在 pdd 达到 100% 的点对数组进行切片,因为我只对之后的点感兴趣。

不幸的是,np.interp 似乎只能在 x 和 y 值都增加的情况下工作。

谁能建议我下一步应该去哪里?

请您参考如下方法:

如果我没理解错的话,您想在 pdd = 50.0 处对函数 depth = f(pdd) 进行插值。出于插值的目的,将 pdd 视为对应于您的“x”值,将 depth 视为对应于您的“y”值可能会有所帮助。

您可以使用 np.argsort按“x”的升序(即升序 pdd)对“x”和“y”进行排序,然后照常使用 np.interp:

# `idx` is an an array of integer indices that sorts `pdd` in ascending order 
idx = np.argsort(pdd) 
 
depth_itp = np.interp([50.0], pdd[idx], depth[idx]) 
 
plt.plot(depth, pdd) 
plt.plot(depth_itp, 50, 'xr', ms=20, mew=2)