显示图表上的线条时,悬停显示系列名称。

huangapple go评论74阅读模式
英文:

bokeh show series name when hovering over line on chart

问题

以下是您要翻译的内容:

"I am plotting a line chart. I use the HoverTool so that when a user hovers over a line they can see the date & value, this bit works. However it doesn't show them the series name, I have tried using the special $name but it just shows three question marks. What am I doing wrong?"

以下是代码部分,无需翻译:

source = ColumnDataSource(data=dict(
    x=[dt.datetime(2023, 1, 1), dt.datetime(2023, 1, 2), dt.datetime(2023, 1, 3), dt.datetime(2023, 1, 4), dt.datetime(2023, 1, 5)],
    y1=[2, 5, 8, 2, 7],
    y2=[3, 2, 1, 4, 7],
    y3=[1, 4, 5, 6, 3]
))

source = ColumnDataSource(data_dict)

tools = "pan,box_zoom,zoom_in,zoom_out,redo,undo,reset,crosshair"
p = figure(title="Blah", 
           x_axis_label='date', 
           width=1600, 
           height=850,
           x_axis_type='datetime',
           tools=tools,
           toolbar_location='above')

for c in data_dict.keys():
    if c != 'x':
        p.line(x='x', y=c, source=source, legend_label=c)

p.legend.location = "top_left"
p.legend.click_policy = "hide"

formatters_tooltips = {'$x': 'datetime'}

p.add_tools(HoverTool(
    tooltips=[('Date', '$x{%Y-%m-d}'), ('Name', '$name'), ('Value', '$y')],
    formatters=formatters_tooltips))

output_file("C:/some_path/some_file.html")

show(p)
英文:

I am plotting a line chart. I use the HoverTool so that when a user hovers over a line they can see the date & the value, this bit works. However it doesn't show them the series name, I have tried using the special $name but it just shows three question marks. What am I doing wrong?

source = ColumnDataSource(data=dict(
x=[dt.datetime(2023, 1, 1), dt.datetime(2023, 1, 2), dt.datetime(2023, 1, 3), dt.datetime(2023, 1, 4), dt.datetime(2023, 1, 5)],
y1=[2, 5, 8, 2, 7],
y2=[3, 2, 1, 4, 7],
y3=[1, 4, 5, 6, 3]
))

source = ColumnDataSource(data_dict)

tools = "pan,box_zoom,zoom_in,zoom_out,redo,undo,reset,crosshair"
p = figure(title = "Blah", 
           x_axis_label='date', 
           width=1600, 
           height=850,
           x_axis_type='datetime',
           tools=tools,
           toolbar_location='above')

for c in data_dict.keys():
    if c != 'x':
        p.line(x='x', y=c, source=source, legend_label=c)

p.legend.location = "top_left"
p.legend.click_policy = "hide"

formatters_tooltips = {'$x': 'datetime'}

p.add_tools(HoverTool(
    tooltips=[('Date', '$x{%Y-%m-%d}'), ('Name', '$name'), ('Value', '$y')],
    formatters=formatters_tooltips))

output_file("C:/some_path/some_file.html")
   
show(p)

答案1

得分: 2

我推断你在工具提示中放置了“Name”占位符,给你的图表添加了标题,但是没有定义$name。也许在你的p.line中添加一个“name=”参数会给你的系列命名。

英文:

I'm deducing you put the 'Name' placeholder in your tooltips, gave your plot a title, but nowhere is $name defined. Perhaps adding a 'name=' parameter to your p.line would name your series.

huangapple
  • 本文由 发表于 2023年2月24日 05:29:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/75550497.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定