问题描述
我一直在努力使情节更加顺畅,就像完成
是模糊的,并建议可能不支持。例如,它没有列在或。 表明它实际上调用了和在如下:
我将遵循的建议,并使用。这是目前提出的方法,它已经很好地记录了和API,它允许默认情况下,独立变量被排除(任何顺序)(请参阅API中的 adopt_sorted
参数)。
>>>来自scipy.interpolate import interp1d
>>>> f = interp1d(X,Y,kind ='quadratic')
>>>> f(Xsmooth)
数组([711.74,720.14123457,726.06049383,729.49777778,
730.45308642,728.92641975,724.91777778,718.4271605,
709.4545679,669]]
如果数据排名不足,也会引发错误。
>>>> f = interp1d(X,Y,kind ='cubic')
I have been trying to make a plot smoother, like it is done here, but my Xs are datetime objects that are not compatible with linspace..
I convert the Xs to matplotlib dates:
Xnew = matplotlib.dates.date2num(X)
X_smooth = np.linspace(Xnew.min(), Xnew.max(), 10)
Y_smooth = spline(Xnew, Y, X_smooth)
But then I get an empty plot, as my Y_smooth is
[ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. ]
for some unknown reason.
How can I make this work ?
EDIT
Here's what I get when I print the variables, i see nothing abnormal :
X : [datetime.date(2016, 7, 31), datetime.date(2016, 7, 30), datetime.date(2016, 7, 29)]
X new: [ 736176. 736175. 736174.]
X new max: 736176.0
X new min: 736174.0
XSMOOTH [ 736174. 736174.22222222 736174.44444444 736174.66666667
736174.88888889 736175.11111111 736175.33333333 736175.55555556
736175.77777778 736176. ]
Y [711.74, 730.0, 698.0]
YSMOOTH [ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
Your X
values are reversed, scipy.interpolate.spline
requires the independent variable to be monotonically increasing, and this method is deprecated - use interp1d
instead (see below).
>>> from scipy.interpolate import spline
>>> import numpy as np
>>> X = [736176.0, 736175.0, 736174.0] # <-- your original X is decreasing
>>> Y = [711.74, 730.0, 698.0]
>>> Xsmooth = np.linspace(736174.0, 736176.0, 10)
>>> spline(X, Y, Xsmooth)
array([ 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.])
reverse X
and Y
first and it works
>>> spline(
... list(reversed(X)), # <-- reverse order of X so also
... list(reversed(Y)), # <-- reverse order of Y to match
... Xsmooth
... )
array([ 698. , 262.18297973, 159.33767533, 293.62017489,
569.18656683, 890.19293934, 1160.79538066, 1285.149979 ,
1167.41282274, 711.74 ])
Note that many spline interpolation methods require X
to be monotonically increasing:
The default order of scipy.interpolate.spline
is cubic. Because there are only 3 data points there are large differences between a cubic spline (order=3
) and a quadratic spline (order=2
). The plot below shows the difference between different order splines; note: 100 points were used to smooth the fitted curve more.
The documentation for scipy.interpolate.spline
is vague and suggests it may not be supported. For example, it is not listed on the scipy.interpolate
main page or on the interploation tutorial. The source for spline
shows that it actually calls spleval
and splmake
which are listed under Additional Tools as:
I would follow cricket_007's suggestion and use interp1d
. It is the currently suggested method, it is very well documented with detailed examples in both the tutorial and API, and it allows the independent variable to be unsorted (any order) by default (see assume_sorted
argument in API).
>>> from scipy.interpolate import interp1d
>>> f = interp1d(X, Y, kind='quadratic')
>>> f(Xsmooth)
array([ 711.74 , 720.14123457, 726.06049383, 729.49777778,
730.45308642, 728.92641975, 724.91777778, 718.4271605 ,
709.4545679 , 698. ])
Also it will raise an error if the data is rank deficient.
>>> f = interp1d(X, Y, kind='cubic')
这篇关于使用样条线和日期时间对象的平滑线不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!