不同线路的多个悬停工具(bokeh)

2024-04-28 22:55:07 发布

您现在位置:Python中文网/ 问答频道 /正文

bokeh图上有多行,我希望HoverTool显示每行的值,但是使用之前stackoverflow答案的方法不起作用:

https://stackoverflow.com/a/27549243/3087409

下面是这个答案的相关代码片段:

fig = bp.figure(tools="reset,hover")
s1 = fig.scatter(x=x,y=y1,color='#0000ff',size=10,legend='sine')
s1.select(dict(type=HoverTool)).tooltips = {"x":"$x", "y":"$y"}
s2 = fig.scatter(x=x,y=y2,color='#ff0000',size=10,legend='cosine')
fig.select(dict(type=HoverTool)).tooltips = {"x":"$x", "y":"$y"}

我的代码是:

^{pr2}$

据我所知,它相当于我链接到的答案中的代码,但是当我将鼠标悬停在图上时,两个悬停工具框都显示相同的值,wind。在


Tags: 答案代码sizetypebokehfigstackoverflowselect
1条回答
网友
1楼 · 发布于 2024-04-28 22:55:07

您需要为每个打印添加渲染器。看看这个。也不要对两个值使用相同的y label来更改名称。在

from bokeh.models import HoverTool
from bokeh.plotting import figure

source = ColumnDataSource(data=df)

plot = figure(x_axis_type='datetime',plot_width=800, plot_height=300)

plot1 =plot.line(x='x',y= 'wind',source=source,color='blue')
plot.add_tools(HoverTool(renderers=[plot1], tooltips=[('wind',"@wind")],mode='vline'))

plot2 = plot.line(x='x',y= 'coal',source=source,color='red')
plot.add_tools(HoverTool(renderers=[plot2], tooltips=[("coal","@coal")],mode='vline'))

show(plot)

输出如下所示。 OUTPUT

相关问题 更多 >