如何使用值映射创建自定义悬停工具 [英] How to create custom hover tool with value mapping
问题描述
我正在尝试创建一个自定义悬停工具,该工具将使用绘图的y值并将该值映射到其他值.
到目前为止,为实现此功能我想出的代码是
来自bokeh.models的 导入HoverTool将hloviews导入为hvdf = pd.DataFrame({零":[0,0,0,0,0,0,0],一个":[1、1、1、1、1、1、1],两个":[2、2、2、2、2、2、2],})映射= {i:c for i,enumerate(df.columns)中的c}def col_mapping(num):返回映射[int(num)]悬停= HoverTool(tooltips = [("x","$ x"),("y","$ y")])img = hv.Image((df.index,np.arange(df.shape [1]),df.T)).opts(tools = [hover])img
x
和 y
将是浮点值.因此,想法是将 y
坐标映射到 mapping
字典
让我知道如何在悬停工具中获取新值,以便当b/w值为0和1时为
谢谢
这是我的处理方式:
code = f返回({json.dumps(mapping)})[Math.floor(special_vars.y)];"悬停= HoverTool(tooltips = [("x","$ x"),("y","$ y"),('mapped_y','$ y {0}')],formatters = {'$ y':CustomJSHover(code = code)})
如果您需要比 col_mapping
更为复杂的代码,则必须使用 ColumnDataSource
并将其添加到完全转换的列中./p>
I am trying to create a custom hover tool using which takes the y-value of the plot and maps the value to different value.
The code I could come up with so far to achieve this functionality is
from bokeh.models import HoverTool
import holoviews as hv
df = pd.DataFrame(
{
"zero": [0, 0, 0, 0, 0, 0, 0],
"one": [1, 1, 1, 1, 1, 1, 1],
"two": [2, 2, 2, 2, 2, 2, 2],
}
)
mapping = {i: c for i, c in enumerate(df.columns)}
def col_mapping(num):
return mapping[int(num)]
hover = HoverTool(tooltips=[("x", "$x"), ("y", "$y")])
img = hv.Image((df.index, np.arange(df.shape[1]), df.T)).opts(tools=[hover])
img
x
and y
will be float values. So the idea is to map the y
coordinates to its corresponding value in the mapping
dictionary
Let me know how I can get a new value in the hover tool so that when the value is b/w 0 and 1 it will be
Thanks
Here's how I'd do it:
code = f"return ({json.dumps(mapping)})[Math.floor(special_vars.y)];"
hover = HoverTool(tooltips=[("x", "$x"), ("y", "$y"), ('mapped_y', '$y{0}')],
formatters={'$y': CustomJSHover(code=code)})
If you need a some more complicated code than that of col_mapping
, then you'd have to use a ColumnDataSource
and just add to it the fully transformed column.
这篇关于如何使用值映射创建自定义悬停工具的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!