python - 散景:无法更新悬停工具提示的格式
问题描述
我正在尝试更新已定义的悬停工具提示的格式,但我没有观察到任何变化。我在下面的示例中所做的更改是更改数字和时间刻度('00:00:00')之间的 x 轴。x 轴按预期更新。使用 Bokeh 版本 0.12.16、mac OS X、Safari 浏览器。
任何关于我做错了什么的提示都值得赞赏。
from bokeh.plotting import figure, ColumnDataSource
from bokeh.models import HoverTool, NumeralTickFormatter, AdaptiveTicker
from bokeh.models.widgets import RadioGroup
from bokeh.layouts import row, widgetbox
from bokeh.io import curdoc
def update_axis_format(new):
if new == 0:
format_num = '0'
mantissas= [1,2,5]
else:
format_num = '00:00:00'
mantissas=[3.6, 7.2, 18]
p.xaxis[0].formatter = NumeralTickFormatter(format = format_num)
p.xaxis.ticker = AdaptiveTicker(base = 10, mantissas = mantissas)
p.xgrid.ticker = AdaptiveTicker(base = 10, mantissas = mantissas)
p.tools[0].tooltips[2] = ("x", "@x{{{}}}".format(format_num))
source = ColumnDataSource(data=dict(
x=[10, 2000, 10000, 40000, 50000],
y=[2, 5, 8, 2, 7],
desc=['A', 'b', 'C', 'd', 'E'],
))
hover = HoverTool(tooltips=[
("index", "$index"),
("desc", "@desc"),
("x", "@x")
])
p = figure(plot_width=400, plot_height=400, tools=[hover],
title="Mouse over the dots")
p.circle('x', 'y', size=20, source=source)
xaxis_format = RadioGroup(
labels=["x-axis as number", "x-axis as time"], active=0)
xaxis_format.on_click(update_axis_format)
widget = widgetbox(xaxis_format)
curdoc().add_root(row(widget,p))
解决方案
BokehJS 代码对tooltips
. 您需要tooltips
完全替换该值。例如,这个简化的代码按预期工作:
def update_axis_format(new):
if new == 0:
format_num = '0'
mantissas= [1,2,5]
else:
format_num = '00:00:00'
mantissas=[3.6, 7.2, 18]
p.xaxis[0].formatter = NumeralTickFormatter(format = format_num)
p.xaxis.ticker = AdaptiveTicker(base = 10, mantissas = mantissas)
p.xgrid.ticker = AdaptiveTicker(base = 10, mantissas = mantissas)
# replace all of tooltips, not just part
p.tools[0].tooltips = [("x", "@x{{{}}}".format(format_num))]
hover = HoverTool(tooltips=[("x", "@x")])
推荐阅读
- node.js - 清理 MongoDB/Node.js/Express 代码和路由
- sql-server - SQL getdate() - 在一个语句中不一样
- r - 使用 ggplot 绘制辅助轴
- c - 关于一个涉及指针的简单神秘函数的两个问题
- typescript - 如何在打字稿中实现具有默认类型参数和默认构造函数参数值的类
- sql - 如何在Excel中将列转换为行
- database - 如何在 Google 表格中进行高级过滤?
- vba - 如果 Dlookup 在多个表中不符合条件,则显示错误。微软访问
- r - [r] raster 包中的镶嵌()中的“缺少乐趣”错误是什么意思?
- excel - 如何在表中找到与条件匹配的第一列