In this lesson you will add hover actions to your last visualization. For this you’ll employ Bokeh’s HoverTool()
to show a tooltip when the cursor crosses paths with a glyph.
To further explore the capabilities of the HoverTool()
, see the HoverTool guide.
File: ThreePointAttVsPctHover.py
# 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.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)
# Add the HoverTool to the figure
fig.add_tools(HoverTool(tooltips=tooltips))
# Visualize
show(fig)
andresgtn on April 2, 2020
would be nice to have like a 3 second delay before switching automatically to the next lesson and allow you to pause it to stay in the current page, like in youtube. The reason for it is that after watching the video, i like to scroll down the page to view the code and type it myself. Its annoying now to be sent to the next video and always have to go and click back.