[需要以下代码的帮助,我的鼠标悬停在???不显示任何数据。我猜是因为我没有正确定义源,或者我需要在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))
当前,您仅对于x和y坐标为directly providing data。 Bokeh对其他数据一无所知。为了使bokeh了解all数据,您必须通过source=source
方法中的vbar
传递源。当您传递源时,bokeh会获取所有数据,以便它可以在悬停时查看不同的列以显示。
[传递源时,您不能直接传递x,顶部和底部坐标,因为散景将不知道如何将这些值与传递的源关联起来¹。因此,当您传递源时,您希望传递x,顶部和底部坐标列的名称,而不是直接传递数据。因此,您想编写类似以下内容的代码:
name1.vbar("index", "open", "close", source=source, fill_color="green", line_color="green")
为此,您需要构造一个已经具有所需数据的Source / DataFrame,而不是在vbar
调用中进行过滤。没有看到您的数据,我无法告诉您如何构造这样的数据框。
1:实际上bokeh通过索引将直接传递的数据关联起来,因此第一个值与源中的第一行关联。