2016-07-31 22 views
1

我一直在努力做圖平滑喜歡做here,但我的兩個X是不與linspace兼容datetime對象..與花鍵+ datetime對象順利行不起作用

我轉換XS到matplotlib日期:

Xnew = matplotlib.dates.date2num(X) 
X_smooth = np.linspace(Xnew.min(), Xnew.max(), 10) 
Y_smooth = spline(Xnew, Y, X_smooth) 

但後來我得到一個空的情節,作爲我Y_smooth是

[ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. ]

˚F或者一些未知的原因。

我該如何做這項工作?

編輯

這裏就是我得到當我打印的變量,我看沒有什麼異常:

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

我只是想一個例子,你的方法爲我工作。嘗試調試每一行。 Xnew.min()和Xnew.max()是什麼值?發佈更多細節。 'X','X_smooth','Xnew'的值是多少?對於[在執行期間在IPython中調試,請嘗試使用'%debug'](https://ipython.org/ipython-doc/1/interactive/tutorial.html#debugging)添加到[添加斷點](https:// docs。 python.org/2/library/pdb.html#debugger-commands),或者可能使用[logging](https://docs.python.org/2/library/logging.html)在執行期間查看中間值。 –

+0

我編輯了問題並添加了變量 – AimiHat

+0

從您的輸出中我推斷出問題在於樣條曲線,而不是日期時間。你能改說你的問題嗎?也許可以用一組較小的數字來嘗試你的問題,比如10而不是300,這樣可以更容易地在SO中顯示,並且更容易調試。仔細檢查'np.splie'文檔 –

回答

3

X值是顛倒的,scipy.interpolate.spline要求自變量是單調遞增,並此方法已棄用 - 請使用interp1d(請參閱下文)。

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

反向XY第一,它的工作原理

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

需要注意的是很多樣條插值方法需要X是單調遞增:

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

x : (N,) array_like - Input dimension of data points – must be increasing

scipy.interpolate.spline默認順序爲立方體。由於只有3個數據點,因此三次樣條(order=3)與二次樣條(order=2)之間存在較大差異。下圖顯示了不同階次樣條之間的差異;注:100分用於平滑擬合曲線更多

scipy.interpolate.spline

的文檔scipy.interpolate.spline是模糊的,並表明,它可能不被支持。例如,它不在scipy.interpolate main pageinterploation tutorial上列出。該source for spline表明,它實際上調用spleval並且被Additional Tools下列爲splmake

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

我會按照cricket_007的建議,並使用interp1d。它是當前建議的方法,它是很好的與detailed examples in both the tutorial和API記錄,並且它允許獨立變量是未排序的(任何順序)由缺省值(參見API assume_sorted參數)。

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

此外,如果數據缺乏等級,則會引發錯誤。

>>> f = interp1d(X, Y, kind='cubic') 

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

+0

順序是否重要?如果你有什麼樣條具有負斜率的直線?X增加爲Y下降... –

+0

我不知道,也許,我沒有閱讀'scipy.interpolate.spline'的文檔,但也沒關係,輸出''np.linspace'是Xsmooth'原始輸入,'Xnew',所以這是個問題,這是不一致的對面。 –

+0

無法解釋..當我做wha你說: 'newX = list(inverse(matplotlib.dates.date2num(X))) Y = list(reversed(Y)) X_smooth = np.linspace(min(newX),max(newX),10 ) Y_smooth =花鍵(下一頁末,Y,X_smooth)' 我得到:'[755 1000.68447671 1062.10559586 991.91003112 842.74445597 667.25554403 518.08996888 447.89440414 509.31552329 755] ' 此Y: '[755.0,755.0,755.0 ]' – AimiHat