python Matplotlib烛台图仅适用于每日数据,不适用于日内 [英] python Matplotlib candlestick plot works only on daily data, not for intraday
问题描述
我正在尝试使用matplotlib绘制烛台数据。从1分钟的数据开始,我使用pd.Timegrouper在不同的时间范围(从5分钟到每天)中对它们进行分组,但是绘图仅适用于每日数据。在下面,您可以找到我正在使用的1分钟数据的示例:
I'm trying to plot candlestick data using matplotlib. Starting from 1 minute data I group them using pd.Timegrouper in various time frame , from 5minute to daily, but plot only works on daily data. Below you can find a sample of the 1 minute data i'm using:
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分钟(其他任何盘中时间段)我都会获得以下结果:
but then when i plot same event on the Daily and 5 minute (ot any other intraday time frame) i obtain following results:
每天(效果不错):
当日(糟糕的结果):
推荐答案
似乎未记录的宽度
的 candlestick_ohlc
参数是关键。将其乘以每个数据点之间一天的比例。由于您的数据以分钟为增量,因此应该这样做:
It seems the undocumented width
argument to candlestick_ohlc
is the key. Multiply it by the fraction of a day between each of your data points. Since your data is in minute increments, this should do:
candlestick_ohlc(ax, tuples, width=.6/(24*60), colorup='g', alpha =.4);
请注意,虽然链接并不明显,但这实际上是一个FAQ。请参阅:
Note this turns out to be an FAQ, though the links are not obvious. See:
- Charting Candlestick_OHLC one minute bars with Pandas and Matplotlib
- Matplotlib Candlestick (Intraday) Chart is One Big Blob
- Matplotlib candlestick in minutes
这篇关于python Matplotlib烛台图仅适用于每日数据,不适用于日内的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!