Dudnikof
Dudnikof

Reputation: 737

Bokeh Charts: HoverTool tips

Bokeh can display on-hover tooltips on the charts, choosing from a list of values. But what if I need to make variable the same for all values? The example below (from the documentation) allows to display value from the list, but if you do desc=foo,, instead of desc=['A', 'b'] tips transforms to "???"

source = ColumnDataSource(
        data=dict(
            x=[1, 2, 3, 4, 5],
            y=[2, 5, 8, 2, 7],
            desc=['A', 'b', 'C', 'd', 'E'],
        )
    )

hover = HoverTool(
        tooltips=[
            ("index", "$index"),
            ("(x,y)", "($x, $y)"),
            ("desc", "@desc"),
        ]
    )

Upvotes: 1

Views: 333

Answers (1)

AndreyIto
AndreyIto

Reputation: 974

The ColumnDataSource data dictionary expects all lists associated with each key to be of the same length. Let's declare some variables before the ColumnDataSource block:

x_ls = [1, 2, 3, 4, 5]
foo_ls = ['foo']*len(x)
print(foo_ls) #['foo', 'foo', 'foo', 'foo', 'foo']

source = ColumnDataSource(
        data=dict(
            x=x_ls,
            y=[2, 5, 8, 2, 7],
            desc=foo_ls
        )
    )

This will display 'foo' for all 5 points, as required. A few other negative cases I tried to illustrate the issue:

desc=foo

This will throw an error, because HoverTool doesn't know what foo is.

desc='foo'

one of the points will show 'f', another two will show 'o', and the last two will show '???'

desc=['foo']

One of the points will show 'foo', and the others, '???'

Upvotes: 2

Related Questions