Force scientific annotation for hover tool

Hi, I want to force all image values as scientific annotation for Hover tool by {0.2e}. But, it doesn’t work.

import numpy as np
import holoviews as hv
from bokeh.models import HoverTool

tooltips = [
    ('x', '$x'),
    ('y', '$y'),
    ('z', '@image{0.2e}'),
]

hover = HoverTool(tooltips=tooltips)
hv.Image(np.random.rand(50,50)*1e5).opts(tools=[hover])

As you can see, it’s still shown as the float format.

It’s funny because if you don’t assign the scientific notation, it becomes scientific notation with 3 digits :slight_smile:

import numpy as np
import holoviews as hv
from bokeh.models import HoverTool
import panel as pn

pn.extension(sizing_mode="stretch_width")

tooltips = [
    ('x', '$x{0.2f}'),
    ('y', '$y{0.2f}'),
    ('z', '@image'),
]

hover = HoverTool(tooltips=tooltips)
img=np.random.rand(50,50)*1e5
plot = hv.Image(img).opts(tools=[hover])
print(img)
pn.panel(plot).servable()

I tried a lot of things and studied Configuring plot tools — Bokeh 2.4.1 Documentation. But could not get it working. It’s should probably be filed as a bug at holoviews github

1 Like

Thanks, switched to Github issue.

1 Like

We can close that again @zxdawn :-). I figured it out after a closer study of the Bokeh docs Configuring plot tools — Bokeh 2.4.1 Documentation.

It turns out that the HoverTool is using a numeral formatting scheme by default. This does not support scientific notation. Instead the printf scheme should be used.

The below works

import holoviews as hv
import numpy as np
from bokeh.models import HoverTool

import panel as pn

pn.extension(sizing_mode="stretch_width")

img=np.random.rand(50,50)*1e5
tooltips = [
    ('x', '$x{0.2f}'),
    ('y', '$y{0.2f}'),
    ('z', '@image{%.2e}'),
]

hover = HoverTool(
    tooltips=tooltips,
    formatters={
        "@image": "printf"
    }
)
plot = hv.Image(img).opts(tools=[hover])
pn.panel(plot).servable()

Oh, I have tried that before, but it didn’t work … That should be caused by too many variables in the notebook, hah. Anyway, thanks for the solution! Close now.

1 Like