英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论