How to create custom hover tool with value mapping

本小妞迷上赌 提交于 2021-02-05 12:13:34

问题


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


回答1:


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.



来源:https://stackoverflow.com/questions/61337080/how-to-create-custom-hover-tool-with-value-mapping

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!