1

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 Answers 2

2

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}'      ),
]
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for your support! The missing curly brackets {} solved the problem!
Just to add that if there is a period, ".", then curly brackets are also needed
0

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')])])

2 Comments

Thanks for your response! I tried your solution and it also works great!
I am glad it worked. Now I would ask you to click the positive tick next to the code in order to have a better reputation. Happy coding!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.