使用一些实验数据,我无法终生弄清楚如何使用 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
这是它的图,y 轴上的深度和 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. ###
我知道我在做一些非常愚蠢的事情,但我就是看不到。
您只需要让您的值从最小到最大 :)。 @a different ben 对你来说应该不是问题,但要注意 future 的读者,depth[indices]
如果 depth 是一个列表而不是一个 numpy 数组!
>>> 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)
向@FerdinandBeyer 致敬argsort
的建议,而不是我丑陋的“压缩值、排序 zip、重新分配值”方法。
我是一名优秀的程序员,十分优秀!