Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Data Science by (50.2k points)

Is it possible to add Tooltips to a Timeseries chart?

In the simplified code example below, I want to see a single column name ('a','b' or 'c') when the mouse hovers over the relevant line.

Instead, a "???" is displayed and ALL three lines get a tooltip (rather than just the one I'm hovering over)

image

Per the documentation ( http://bokeh.pydata.org/en/latest/docs/user_guide/tools.html#hovertool), field names starting with “@” are interpreted as columns on the data source.

How can I display the 'columns' from a pandas dataframe in the tooltip?

Or, if the high-level TimeSeries interface doesn't support this, any clues for using the lower-level interfaces to do the same thing? (line? multi_line?) or convert the DataFrame into a different format (ColumnDataSource?)

For bonus credit, how should the "$x" be formated to display the date as a date?

thanks in advance

    import pandas as pd

    import numpy as np

    from bokeh.charts import TimeSeries

    from bokeh.models import HoverTool

    from bokeh.plotting import show

    toy_df = pd.DataFrame(data=np.random.rand(5,3), columns = ('a', 'b' ,'c'), index = pd.DatetimeIndex(start='01-01-2015',periods=5, freq='d'))   

    p = TimeSeries(toy_df, tools='hover')  

    hover = p.select(dict(type=HoverTool))

    hover.tooltips = [

        ("Series", "@columns"),

        ("Date", "$x"),

        ("Value", "$y"),

        ]

    show(p)

1 Answer

0 votes
by (107k points)

I inspected the glyph render data source to see what are the names on it. Then I apply those names on the hoover tooltips. You can see the resulting plot in the following link:

import numpy as np

from bokeh.charts import TimeSeries

from bokeh.models import HoverTool

from bokeh.plotting import show

toy_df = pd.DataFrame(data=np.random.rand(5,3), columns = ('a', 'b' ,'c'), index = pd.DatetimeIndex(start='01-01-2015',periods=5, freq='d'))   

#Bockeh display dates as numbers so convert to string tu show correctly

toy_df.index = toy_df.index.astype(str) 

p = TimeSeries(toy_df, tools='hover')  

#Next 3 lines are to investigate how are names on gliph to call them with @name on hover

#glyph_renderers = p.select(dict(type=GlyphRenderer))

#bar_source = glyph_renderers[0].data_source

#print(bar_source.data)  #Here we can inspect names to call on hover


 

hover = p.select(dict(type=HoverTool))

hover.tooltips = [

        ("Series", "@series"),

        ("Date", "@x_values"),

        ("Value", "@y_values"),

        ]

show(p)

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...