Search code examples
holoviews

Hover tool - Dataframes with spaces in column names don't work in hover?


I have noticed that if I try to use hover tool in a Holoviews plot, column names with spaces in them don't work in the tooltip. In Bokeh, you are permitted to use spaces as long as you enclose the column names in curly braces when creating the hover object. But that doesn't seem to work in Holoviews. In the example below, col2 and col3 values show up correctly in the hover tooltip but col 1 shows up as ????

df = pd.DataFrame({'col 1': [1, 2, 3, 4, 5], 'col2': [2, 5, 8, 2, 7], 'col3': ['A', 'b', 'C', 'd', 'E']})
df

hover = HoverTool(tooltips=[
    ("index", "$index"),
    ("col 1", "@{col 1}{0.0}"),
    ("col2", "@col2"),
    ("col3", "@col3"),
])

bars = hv.Bars(df, kdims=["col 1"], vdims=['col2',col3']).opts(plot=dict(tools=[hover]))
bars

Am I missing something or do I have to rename all my column names to remove spaces?


Solution

  • HoloViews is internally escaping columns with spaces in them. If you change the column reference to @{col_1} it should work:

    hover = HoverTool(tooltips=[
        ("index", "$index"),
        ("col 1", "@{col_1}{0.0}"),
        ("col2", "@col2"),
        ("col3", "@col3"),
    ])