散景 / Python TOOLTIPS 问题 / 悬停
Bokeh / Python issue with TOOLTIPS / Hover over
需要以下代码的帮助,我的悬停没有显示任何数据,只是???。我猜是因为我没有正确定义源或者我需要在 vbar 代码中包含一个参数。我是否需要向来源添加更多信息,例如列名等,还是我还需要引用 vbar 参数中的源名称和列名称?
谢谢
def get_width():
mindate = df['local_time'].min()
maxdate = df['local_time'].max()
return 0.8 * (maxdate-mindate).total_seconds()*1000 / len(df['local_time'])
plots = []
sliders = []
for t in df['timeframeID'].unique():
inc = df[df['timeframeID'] == t].close > df[df['timeframeID'] == t].open
dec = df[df['timeframeID'] == t].open > df[df['timeframeID'] == t].close
source = ColumnDataSource(data=df)
TOOLS = "pan,wheel_zoom,box_zoom,crosshair,reset,save"
TOOLTIPS = [('open', '@open'),('high', '@high'),('low', '@low'),('close', '@close')]
name1= figure(plot_width=1600, plot_height = 900, title="Instrument AUDUSD: "+t, tools = TOOLS, tooltips=TOOLTIPS)
name1.xaxis.major_label_overrides = {
i: date.strftime('%b %d') for i, date in enumerate(pd.to_datetime(df["local_time"]))
}
name1.xaxis.bounds = (0, df.index[-1])
name1.segment(df[df['timeframeID'] == t].index[inc], df[df['timeframeID'] == t].high[inc],
df[df['timeframeID'] == t].index[inc],df[df['timeframeID'] == t].low[inc], color="black")
name1.segment(df[df['timeframeID'] == t].index[dec], df[df['timeframeID'] == t].high[dec],
df[df['timeframeID'] == t].index[dec],df[df['timeframeID'] == t].low[dec], color="black")
#name1.y_range.range_padding = 0.05
name1.vbar(df[df['timeframeID']== t].index[inc], 0.5, df[df['timeframeID']== t].open[inc], df[df['timeframeID']== t].close[inc],
fill_color="green", line_color="green")#, width=get_width())
name1.vbar(df[df['timeframeID']== t].index[dec], 0.5, df[df['timeframeID']== t].open[dec], df[df['timeframeID']== t].close[dec],
fill_color="#F2583E", line_color="#F2583E")#, width=get_width())
r = name1.circle(df[df['timeframeID']== t].index, df[df['timeframeID']== t].AV, alpha = 1, radius = .20)
name1.y_range.range_padding = 0.05
callback = CustomJS(args=dict(renderer=r), code="""
renderer.glyph.radius = cb_obj.value;
""")
s = Slider(start=0, end=1.5, value=.20, step=.05, title="Radius - " + t)
s.js_on_change('value', callback)
output_notebook()
output_file("candlestick.html", title="candlestick.py example")
sliders.append(s)
plots.append(name1)
show(column(
row(
*plots),*sliders))
这是我的数据框 df 的样子:
目前您 directly providing data 仅针对 x 和 y 坐标。 Bokeh 对其他数据一无所知。要使散景感知 所有 数据,您必须在 vbar
方法中通过 source=source
传递源。当您传递源时,bokeh 会获取所有数据,以便它可以查看不同的列以在悬停时显示。
当您传递源时,您不能直接传递 x、顶部和底部坐标,否则 bokeh 将不知道如何将这些值与您传递的源相关联¹。所以当你传递一个源时,你想传递x、顶部和底部坐标列的名称,而不是直接传递数据。所以你想写这样的东西:
name1.vbar("index", "open", "close", source=source, fill_color="green", line_color="green")
为此,您需要构建一个 source/DataFrame,其中已经包含您想要的数据,而不是像在 vbar
调用中那样进行过滤。如果没有看到您的数据,我无法告诉您如何构建这样的 Dataframe。
1:实际上bokeh通过索引关联直接传递的数据,因此第一个值与源中的第一行关联。
谢谢
def get_width():
mindate = df['local_time'].min()
maxdate = df['local_time'].max()
return 0.8 * (maxdate-mindate).total_seconds()*1000 / len(df['local_time'])
plots = []
sliders = []
for t in df['timeframeID'].unique():
inc = df[df['timeframeID'] == t].close > df[df['timeframeID'] == t].open
dec = df[df['timeframeID'] == t].open > df[df['timeframeID'] == t].close
source = ColumnDataSource(data=df)
TOOLS = "pan,wheel_zoom,box_zoom,crosshair,reset,save"
TOOLTIPS = [('open', '@open'),('high', '@high'),('low', '@low'),('close', '@close')]
name1= figure(plot_width=1600, plot_height = 900, title="Instrument AUDUSD: "+t, tools = TOOLS, tooltips=TOOLTIPS)
name1.xaxis.major_label_overrides = {
i: date.strftime('%b %d') for i, date in enumerate(pd.to_datetime(df["local_time"]))
}
name1.xaxis.bounds = (0, df.index[-1])
name1.segment(df[df['timeframeID'] == t].index[inc], df[df['timeframeID'] == t].high[inc],
df[df['timeframeID'] == t].index[inc],df[df['timeframeID'] == t].low[inc], color="black")
name1.segment(df[df['timeframeID'] == t].index[dec], df[df['timeframeID'] == t].high[dec],
df[df['timeframeID'] == t].index[dec],df[df['timeframeID'] == t].low[dec], color="black")
#name1.y_range.range_padding = 0.05
name1.vbar(df[df['timeframeID']== t].index[inc], 0.5, df[df['timeframeID']== t].open[inc], df[df['timeframeID']== t].close[inc],
fill_color="green", line_color="green")#, width=get_width())
name1.vbar(df[df['timeframeID']== t].index[dec], 0.5, df[df['timeframeID']== t].open[dec], df[df['timeframeID']== t].close[dec],
fill_color="#F2583E", line_color="#F2583E")#, width=get_width())
r = name1.circle(df[df['timeframeID']== t].index, df[df['timeframeID']== t].AV, alpha = 1, radius = .20)
name1.y_range.range_padding = 0.05
callback = CustomJS(args=dict(renderer=r), code="""
renderer.glyph.radius = cb_obj.value;
""")
s = Slider(start=0, end=1.5, value=.20, step=.05, title="Radius - " + t)
s.js_on_change('value', callback)
output_notebook()
output_file("candlestick.html", title="candlestick.py example")
sliders.append(s)
plots.append(name1)
show(column(
row(
*plots),*sliders))
这是我的数据框 df 的样子:
目前您 directly providing data 仅针对 x 和 y 坐标。 Bokeh 对其他数据一无所知。要使散景感知 所有 数据,您必须在 vbar
方法中通过 source=source
传递源。当您传递源时,bokeh 会获取所有数据,以便它可以查看不同的列以在悬停时显示。
当您传递源时,您不能直接传递 x、顶部和底部坐标,否则 bokeh 将不知道如何将这些值与您传递的源相关联¹。所以当你传递一个源时,你想传递x、顶部和底部坐标列的名称,而不是直接传递数据。所以你想写这样的东西:
name1.vbar("index", "open", "close", source=source, fill_color="green", line_color="green")
为此,您需要构建一个 source/DataFrame,其中已经包含您想要的数据,而不是像在 vbar
调用中那样进行过滤。如果没有看到您的数据,我无法告诉您如何构建这样的 Dataframe。
1:实际上bokeh通过索引关联直接传递的数据,因此第一个值与源中的第一行关联。