【问题标题】:python Matplotlib candlestick plot works only on daily data, not for intradaypython Matplotlib烛台图仅适用于每日数据,不适用于日内数据
【发布时间】:2016-04-24 13:38:45
【问题描述】:

我正在尝试使用 matplotlib 绘制烛台数据。从 1 分钟的数据开始,我使用 pd.Timegrouper 在不同的时间范围内对它们进行分组,从 5 分钟到每天,但绘图仅适用于每日数据。您可以在下面找到我正在使用的 1 分钟数据示例:

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

但是当我在 Daily 和 5 分钟(其他任何日内时间框架)上绘制相同的事件时,我得到以下结果:

每日(好结果):

盘中(坏结果):

【问题讨论】:

  • 可以找到一个可能的解决方案here:

标签: python matplotlib candlestick-chart


【解决方案1】:

似乎candlestick_ohlc 的未记录的width 参数是关键。将其乘以每个数据点之间一天的分数。由于您的数据以分钟为单位,因此应该这样做:

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

请注意,这是一个常见问题解答,尽管链接并不明显。见:

【讨论】:

    猜你喜欢
    • 2017-11-08
    • 1970-01-01
    • 1970-01-01
    • 2017-01-12
    • 2023-02-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多