2016-04-24 93 views
1

我試圖用matplotlib繪製燭臺數據。從1分鐘的數據開始,我使用pd.Timegrouper在不同的時間範圍內對它們進行分組,從5分鐘到每日,但情節僅適用於日常數據。下面你可以找到1分鐘數據的樣本我使用:python Matplotlib燭臺陰謀只適用於日常數據,不適用於盤中

**Data sample:** (pandas dataframe)

data_indexed_5M = data_indexed.groupby([pd.TimeGrouper(freq=pd.offsets.Minute('5'))]).agg({'<LOW>': lambda s: s.min(), 
            '<HIGH>': lambda s: s.max(), 
            '<OPEN>': lambda s: s[0], 
            '<CLOSE>': lambda s: s[-1]}) 

ata_indexed_Daily = data_indexed.groupby([pd.TimeGrouper(freq='D')]).agg({'<LOW>': lambda s: s.min(), 
            '<HIGH>': lambda s: s.max(), 
            '<OPEN>': lambda s: s[0], 
            '<CLOSE>': lambda s: s[-1]}) 

data_indexed_Daily['Date2'] = data_indexed_Daily['dateTime'].apply(lambda d: mdates.date2num(d.to_pydatetime())) 
data_indexed_Daily = data_indexed_Daily.set_index('dateTime') 

data_indexed_5M['Date2'] = data_indexed_5M['dateTime'].apply(lambda d: mdates.date2num(d.to_pydatetime())) 
data_indexed_5M = data_indexed_5M.set_index('dateTime') 


def plotWithMatplot(dataevento): 
    deltatime = timedelta(minutes=100*5) #...(days=100) for daily plot 

    pre_data = dataevento - deltatime 
    post_data= dataevento + deltatime 

    data_slice = data_indexed_5M.loc[pre_data:post_data] #data_indexed_Daily --> for daily plot 

    tuples = [tuple(x) for x in  data_slice[['Date2','<OPEN>','<HIGH>','<LOW>','<CLOSE>']].values] 

    fig, ax = plt.subplots() 
    ax.xaxis_date() 
    ax.xaxis.set_major_formatter(mdates.DateFormatter("%Y-%m-%d %H:%M:"))  

    plt.xticks(rotation=45) 
    plt.xlabel("Date") 
    plt.ylabel("Price") 
    plt.title("EURUSD 5M") 
    candlestick_ohlc(ax, tuples, width=.6, colorup='g', alpha =.4); 

    plt.show() 

但後來當我繪製的日常和5分鐘同一事件(OT任何其他盤中的時限)我得到如下結果:

日報(好成績):

enter image description here

即日(壞的結果):

enter image description here

+0

一種可能的方案,可以發現在[此處](http://stackoverflow.com/questions/36846032/matplotlib-candlestick-intraday- chart-is-one-big-blob#): – jcsun

回答

1

看來無證width參數candlestick_ohlc是關鍵。將它乘以每個數據點之間的一天的一小部分。由於你的數據是以微小的增量,這應該這樣做:

candlestick_ohlc(ax, tuples, width=.6/(24*60), colorup='g', alpha =.4); 

注意這原來是一個常見問題,雖然鏈接不明顯。參見:

+0

非常有幫助,非常感謝! – jcsun

相關問題