Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set your subtitle preferences in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please refer to our video player troubleshooting guide for assistance.

Emphasizing With Hover Inspections

In this lesson you will emphasize the hover actions you implemented in the last lesson. You will add an additional glyph, in this case circles instead of squares, with an initial opacity set to zero so that it is invisible until the cursor is touching it.

For even more exploration here is a link to the user guide section on Hover Inspections.

File: ThreePointAttVsPctHoverGlyph.py

Python
# Bokeh Libraries
from bokeh.plotting import figure, show
from bokeh.io import output_file
from bokeh.models import ColumnDataSource, NumeralTickFormatter, HoverTool

# Import the data
from read_nba_data import three_takers

# Output to file
output_file('three_point_att_vs_pct_hoverglyph.html',
            title='Three-Point Attempts vs. Percentage')

# Store the data in a ColumnDataSource
three_takers_cds = ColumnDataSource(three_takers)

#Specify the selection tools to be made available
select_tools = ['box_select', 'lasso_select', 'poly_select', 'tap', 'reset']

# Format the tooltip
tooltips = [
            ('Player', '@name'),
            ('Three-Pointers Made', '@play3PM'),
            ('Three-Pointers Attempted', '@play3PA'),
            ('Three-Point Percentage', '@pct3PM{00.0%}')   
           ]

# Create the figure
fig = figure(plot_height=400,
             plot_width=600,
             x_axis_label='Three-Point Shots Attempted',
             y_axis_label='Percentage Made',
             title='3PT Shots Attempted vs. Percentage Made (min. 100 3PA), 2017-18',
             toolbar_location='below',
             tools=select_tools)

# Format the y-axis tick label as percentages
fig.yaxis[0].formatter = NumeralTickFormatter(format='00.0%')

# Add square representing each player
fig.square(x='play3PA',
           y='pct3PM',
           source=three_takers_cds,
           color='royalblue',
           selection_color='deepskyblue',
           nonselection_color='lightgray',
           nonselection_alpha=0.3)

# Configure a renderer to be used upon hover
hover_glyph = fig.circle(x='play3PA', y='pct3PM', source=three_takers_cds,
                         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)

00:00 If you want to further emphasize the players when you’re hovering, Bokeh makes it possible using something called hover inspections. It’s going to change the type of glyph as you hover over the items to make it stand out just a little bit more. You’re going to change this Hover to say HoverGlyph. Make a copy, and just add the word Glyph to it. Okay. So, what’s new?

00:28 You’re still going to have the same tooltips that you created earlier, and the figure and everything else is going to stay the same. What you’re going to do is change the type of glyph before you add the HoverTool. That’s going to be done right here.

00:48 So, the hover glyph will be a circle, a circular glyph.

01:09 size is going to be 15 and an alpha of 0, but when hovering, the hover_fill_color is going to be 'black', with a hover_alpha of 0.5. Okay. So again, the glyph, x, y, the .square() is still there.

01:30 It’s exactly the same x and y and source. But what you’re changing here is the size of the glyph and its alpha so that it’ll be a size 15 circle, which will by default be transparent. And then when you hover, it will be colored black with an alpha of 0.5.

01:49 So then when you add the tools here, tooltips will stay the same, but you’re going to add what’s called a renderer, and that’s where you select the hover_glyph. Now, if I save and run this version… Oh, I should change the name of the HTML file.

02:17 Okay. Saving that, now run the script.

02:31 So now as you hover, you’ll see the circular glyph surrounding your original blue square, highlighting that that particular piece of data is being called out, which can be super useful if you have a lot of information on the screen. In your next video, you’ll work with some new data and also start linking axes across multiple visualizations.

Become a Member to join the conversation.