2017-06-15 101 views
1

當試圖繪製的大熊貓一個滾動平均值用下面的代碼我得到一個奇怪的出現圖圖形與滾動平均值數據不平滑正常

data['mean_Kincaid'] = pd.rolling_mean(data.Kincaid,30, min_periods=1) 
data['Year']= data['Date'].dt.year 
data.plot(x='Date', y='mean_Kincaid') 

其中產量如下圖平滑我的數據:enter image description here

我希望圖更「流暢」(我的目標是使用rolling_mean函數開始)。

任何幫助,將不勝感激:)

更新:圖像的推薦代碼:enter image description here

更新2:用下面的代碼我是能夠產生以下圖片 - 就如何解決任何想法X軸只是一年?

data['mean_Kincaid'] = data.Kincaid.rolling(75, min_periods=1).mean() 
data.plot(x='Date', y='mean_Kincaid') 

enter image description here

當我用下面的代碼運行它,我得到的錯誤「AttributeError的:只能使用.DT訪問與datetimelike值」謝謝!

更新3:

data['mean_Kincaid'] = data.Kincaid.rolling(10000, 
min_periods=1).mean() 
data.Date = pd.to_datetime(data.Date) 
data.plot(x='Date', y='mean_Kincaid', legend=False, title="Kincaid 
scores over time") 

enter image description here

回答

1

這是平滑不足。

n = 8001 
df = pd.DataFrame(dict(
     Kincaid=np.sin(np.linspace(-4, 4, n)) + np.random.rand(n) * 2, 
     Date=pd.date_range('2010-03-31', periods=n) 
    )) 

df['mean_Kincaid'] = df.Kincaid.rolling(30, min_periods=1).mean() 

df.plot(x='Date', y=['Kincaid', 'mean_Kincaid']) 

enter image description here

這是更好

df['mean_Kincaid'] = df.Kincaid.rolling(360, min_periods=1).mean() 

df.plot(x='Date', y=['Kincaid', 'mean_Kincaid']) 

enter image description here

通知所述較大的窗參數。

+0

但是,當我運行代碼時,我得到了這個看起來很醜陋的東西(我把它作爲更新添加到了我的問題的底部) - 是否有一種方法可以使它看起來更像您的?我嘗試過玩弄窗戶無濟於事! –

+0

也許帶重採樣() –

+0

@GrahamStreich我不知道你的數據是什麼樣子。你可能有非常高的頻率,超過30個週期是不夠的。我會更新我的帖子。 – piRSquared