如何使用scipy.interpolate.splrep插入曲线?

a d*_*ben 3 python interpolation scipy

使用一些实验数据,我不能为我的生活弄清楚如何使用splrep来创建B样条.数据如下:http://ubuntuone.com/4ZFyFCEgyGsAjWNkxMBKWD

这是一段摘录:

#Depth  Temperature
1   14.7036
-0.02   14.6842
-1.01   14.7317
-2.01   14.3844
-3  14.847
-4.05   14.9585
-5.03   15.9707
-5.99   16.0166
-7.05   16.0147
Run Code Online (Sandbox Code Playgroud)

这是一个关于x的深度和温度的情节: 在此输入图像描述

这是我的代码:

import numpy as np
from scipy.interpolate import splrep, splev

tdata = np.genfromtxt('t-data.txt', 
                      skip_header=1, delimiter='\t')
depth = tdata[:, 0]
temp = tdata[:, 1]

# Find the B-spline representation of 1-D curve:
tck = splrep(depth, temp)
### fails here with "Error on input data" returned. ###
Run Code Online (Sandbox Code Playgroud)

我知道我正在做一些愚蠢的事,但我只是看不到它.

Nol*_*lty 6

你只需要从最小到最大的值:).对于你来说这不应该是一个问题@a ben ben,但是要注意未来的读者,如果深度是一个列表而不是一个numpy数组depth[indices]会抛出一个TypeError!

>>> indices = np.argsort(depth)
>>> depth = depth[indices]
>>> temp = temp[indices]
>>> splrep(depth, temp)
(array([-7.05, -7.05, -7.05, -7.05, -5.03, -4.05, -3.  , -2.01, -1.01,
        1.  ,  1.  ,  1.  ,  1.  ]), array([ 16.0147    ,  15.54473241,  16.90606794,  14.55343229,
        15.12525673,  14.0717599 ,  15.19657895,  14.40437622,
        14.7036    ,   0.        ,   0.        ,   0.        ,   0.        ]), 3)
Run Code Online (Sandbox Code Playgroud)

帽子提示给@FerdinandBeyer建议argsort而不是我的丑陋"压缩值,拉链,重新赋值"方法.

  • 您应该使用`np.argsort()`函数对数据进行排序:`indices = np.argsort(depth); depth = depth [indices]; temp = temp [indices]`.这样就可以节省你的元组列表. (2认同)