简体   繁体   English

用于不同线条(散景)的多个HoverTools

[英]Multiple HoverTools for different lines (bokeh)

I have more than one line on a bokeh plot, and I want the HoverTool to show the value for each line, but using the method from a previous stackoverflow answer isn't working: 我在散景图上有多条线,并且我希望HoverTool显示每条线的值,但是使用上一个stackoverflow答案中的方法不起作用:

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

Here's the relevant code snippet from that answer: 这是该答案的相关代码段:

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"}

And here's my code: 这是我的代码:

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

source = ColumnDataSource(data=dict(
    x = [list of datetimes]
    wind = [some list]
    coal = [some other list]
    )
)

hover = HoverTool(mode = "vline")

plot = figure(tools=[hover], toolbar_location=None,
    x_axis_type='datetime')

plot.line('x', 'wind')
plot.select(dict(type=HoverTool)).tooltips = {"y":"@wind"}
plot.line('x', 'coal')
plot.select(dict(type=HoverTool)).tooltips = {"y":"@coal"}

As far as I can tell, it's equivalent to the code in the answer I linked to, but when I hover over the figure, both hover tools boxes show the same value, that of the wind . 据我所知,它等效于我链接到的答案中的代码,但是当我将鼠标悬停在该图上时,两个悬停工具框都显示相同的值,即wind值。

You need to add renderers for each plot. 您需要为每个图添加渲染器。 Check this. 检查一下。 Also do not use same y label for both values change the names. 也不要为两个值更改名称使用相同的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)

The output look like this. 输出看起来像这样。 输出值

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM