Smooth line with spline + datetime objects doesn't work

别等时光非礼了梦想. 提交于 2019-12-01 05:29:56

问题


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.]

回答1:


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:

  • UnivariateSpline

x : (N,) array_like - 1-D array of independent input data. Must be increasing.

  • InterpolatedUnivariateSpline

x : (N,) array_like - Input dimension of data points – must be 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.splineis 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:

Functions existing for backward compatibility (should not be used in new code).

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')

ValueError: x and y arrays must have at least 4 entries



来源:https://stackoverflow.com/questions/38686415/smooth-line-with-spline-datetime-objects-doesnt-work

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!