1
votes

1st post here and new to python so go easy on me!

I have plotted my visualisation in python using bokeh library. I've used the hovertool function but cant seem to display any data when I hover over it. Not sure where I am going wrong here. Any support welcome.

Bokeh Libraries

from bokeh.plotting import figure, show

from bokeh.io import output_file

from bokeh.models import ColumnDataSource, NumeralTickFormatter

from bokeh.models import HoverTool

output_notebook()

Store the data in a ColumnDataSource

df = ColumnDataSource(contents)

Specify the selection tools to be made available

select_tools = ['box_select', 'lasso_select', 'poly_select', 'tap', 'reset']

Create the figure

fig = figure(plot_height=400,
         plot_width=600,
         x_axis_label=' Store Average Age of Respondent',
         y_axis_label=' Store NPS Percentage Score %',
         title='MNWIOM Average Customer Age Vs. NPS Percentage Score by Store',
         toolbar_location='below',
         tools=select_tools)

Add square representing each store

fig.square(x='Average Age of Respondent',
       y='NPS Percentage Score',
       source=df,
       color='royalblue',
       selection_color='deepskyblue',
       nonselection_color='lightgray',
        nonselection_alpha=0.3)

Format the tooltip

tooltips = [('Store','@Store Name'),
        ('Average Customer Age', '@Average Age of Respondent'),
        ('NPS Score %', '@NPS Percentage Score'),]

Configure a renderer to be used upon hover

hover_glyph = fig.circle(x='Average Age of Respondent', y='NPS Percentage Score', source=df,
                     size=15, alpha=0,
                     hover_fill_color='black', hover_alpha=0.5)

Add the HoverTool to the figure

fig.add_tools(HoverTool(tooltips=tooltips, renderers=[hover_glyph]))

Visualize

show(fig)
2

2 Answers

1
votes

If your fieldnames have spaces, you have to use curly brackets {} in your tooltips definition. From the hovertool docs (link):

tooltips=[
    ( 'date',   '@date{%F}'            ),
    ( 'close',  '$@{adj close}{%0.2f}' ), # use @{ } for field names with spaces
    ( 'volume', '@volume{0.00 a}'      ),
]
0
votes

I would use the Hovertool inside the figure variable.

fig = figure(plot_height=400,
         plot_width=600,
         x_axis_label=' Store Average Age of Respondent',
         y_axis_label=' Store NPS Percentage Score %',
         title='MNWIOM Average Customer Age Vs. NPS Percentage Score by Store',
         toolbar_location='below',
         tools=[HoverTool(tooltips=[('Store','@Store Name'),('Average Customer Age', 
        '@Average Age of Respondent'),('NPS Score %', '@NPS Percentage Score')])])