当数据丢失时,散景消除日期时间轴中的间隙
Bokeh remove gaps in datatime axis when date is mising
我试图用我拥有的 OHLC 数据绘制烛台。数据来自5分钟timeframe resample to 4 hour timeframe,所以周末会有很大差距。
# Load data
subdata = pd.read_csv(
'data/M5/EURUSD.csv',
header = None,
skiprows = 0,
sep = '\t',
names = [
'date',
'open',
'high',
'low',
'close',
'volume'
],
)
subdata['date'] = pd.to_datetime(subdata['date'])
subdata.set_index(['date'], inplace = True)
# Resample
subdata = subdata.resample('4H').agg({
'open': 'first',
'high': 'max',
'low': 'min',
'close': 'last',
'volume': 'sum'
}).dropna(axis=0)
在我对数据重新采样之后,然后使用 Bokeh 绘制数据,这就出现了问题,即周末的差距。这是我用来绘制数据的代码,并使用 concept 解决了这个问题,但仍然无法正常工作。
fig1 = figure(x_axis_type='datetime', height=400, width=900)
# I try to add this code but still not work
fig1.xaxis.major_label_overrides = {
i: date.strftime('%Y-%m-%d %H:%S') for i, date in enumerate(subdata.index)
}
wide = 12*60*60*200
inc = subdata['close'] > subdata['open']
dec = subdata['open'] > subdata['close']
fig1.segment(subdata.index, subdata['high'], subdata.index, subdata['low'], color='black')
fig1.vbar(subdata.index[inc], wide, subdata['open'][inc], subdata.close[inc], fill_color='#D5E1DD', line_color='black')
fig1.vbar(subdata.index[dec], wide, subdata['open'][dec], subdata['close'][dec], fill_color='#F2583E', line_color='black')
show(gridplot([[fig1]]))
这是结果
是我的代码有问题还是我的概念有误?
经过反复试验,终于找到了问题的根源。当将 xaxis 更改为 enumerate(subdata.index)
时,这意味着 xaxis 使用数字而不是日期时间。但我仍然使用 datetime 来制作本应使用数字的图,奇怪的事情来了。为什么 bokeh 仍然在 xaxis 数字上接收 xaxis 日期时间,这最终会造成间隙和错误的绘图?
要解决此问题,需要行中的索引号。在我的例子中,索引使用日期时间,因此需要为索引号创建一个新列,然后创建一个带有索引号的图。
# Here code to make number index
subdata['x'] = subdata.reset_index().index
fig1 = figure(x_axis_type='datetime', height=400, width=900)
fig1.xaxis.major_label_overrides = {
i: date.strftime('%Y-%m-%d %H:%S') for i, date in enumerate(subdata.index)
}
wide = 0.5
inc = subdata['close'] > subdata['open']
dec = subdata['open'] > subdata['close']
fig1.segment(subdata['x'], subdata['high'], subdata['x'], subdata['low'], color='black')
fig1.vbar(subdata['x'][inc], wide, subdata['open'][inc], subdata['close'][inc], fill_color='#D5E1DD', line_color='black')
fig1.vbar(subdata['x'][dec], wide, subdata['open'][dec], subdata['close'][dec], fill_color='#F2583E', line_color='black')
show(gridplot([[fig1]]))
我试图用我拥有的 OHLC 数据绘制烛台。数据来自5分钟timeframe resample to 4 hour timeframe,所以周末会有很大差距。
# Load data
subdata = pd.read_csv(
'data/M5/EURUSD.csv',
header = None,
skiprows = 0,
sep = '\t',
names = [
'date',
'open',
'high',
'low',
'close',
'volume'
],
)
subdata['date'] = pd.to_datetime(subdata['date'])
subdata.set_index(['date'], inplace = True)
# Resample
subdata = subdata.resample('4H').agg({
'open': 'first',
'high': 'max',
'low': 'min',
'close': 'last',
'volume': 'sum'
}).dropna(axis=0)
在我对数据重新采样之后,然后使用 Bokeh 绘制数据,这就出现了问题,即周末的差距。这是我用来绘制数据的代码,并使用 concept 解决了这个问题,但仍然无法正常工作。
fig1 = figure(x_axis_type='datetime', height=400, width=900)
# I try to add this code but still not work
fig1.xaxis.major_label_overrides = {
i: date.strftime('%Y-%m-%d %H:%S') for i, date in enumerate(subdata.index)
}
wide = 12*60*60*200
inc = subdata['close'] > subdata['open']
dec = subdata['open'] > subdata['close']
fig1.segment(subdata.index, subdata['high'], subdata.index, subdata['low'], color='black')
fig1.vbar(subdata.index[inc], wide, subdata['open'][inc], subdata.close[inc], fill_color='#D5E1DD', line_color='black')
fig1.vbar(subdata.index[dec], wide, subdata['open'][dec], subdata['close'][dec], fill_color='#F2583E', line_color='black')
show(gridplot([[fig1]]))
这是结果
是我的代码有问题还是我的概念有误?
经过反复试验,终于找到了问题的根源。当将 xaxis 更改为 enumerate(subdata.index)
时,这意味着 xaxis 使用数字而不是日期时间。但我仍然使用 datetime 来制作本应使用数字的图,奇怪的事情来了。为什么 bokeh 仍然在 xaxis 数字上接收 xaxis 日期时间,这最终会造成间隙和错误的绘图?
要解决此问题,需要行中的索引号。在我的例子中,索引使用日期时间,因此需要为索引号创建一个新列,然后创建一个带有索引号的图。
# Here code to make number index
subdata['x'] = subdata.reset_index().index
fig1 = figure(x_axis_type='datetime', height=400, width=900)
fig1.xaxis.major_label_overrides = {
i: date.strftime('%Y-%m-%d %H:%S') for i, date in enumerate(subdata.index)
}
wide = 0.5
inc = subdata['close'] > subdata['open']
dec = subdata['open'] > subdata['close']
fig1.segment(subdata['x'], subdata['high'], subdata['x'], subdata['low'], color='black')
fig1.vbar(subdata['x'][inc], wide, subdata['open'][inc], subdata['close'][inc], fill_color='#D5E1DD', line_color='black')
fig1.vbar(subdata['x'][dec], wide, subdata['open'][dec], subdata['close'][dec], fill_color='#F2583E', line_color='black')
show(gridplot([[fig1]]))