-
Notifications
You must be signed in to change notification settings - Fork 5.3k
/
Copy pathinteractive-legend.py
72 lines (63 loc) · 1.98 KB
/
interactive-legend.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# Bokeh Libraries
from bokeh.io import output_file
from bokeh.layouts import row
from bokeh.models import CDSView, ColumnDataSource, GroupFilter
from bokeh.plotting import figure, show
# Output inline in the notebook
output_file("lebron-vs-durant.html", title="LeBron James vs. Kevin Durant")
# Store the data in a ColumnDataSource
player_gm_stats = ColumnDataSource(player_stats) # noqa
# Create a view for each player
lebron_filters = [
GroupFilter(column_name="playFNm", group="LeBron"),
GroupFilter(column_name="playLNm", group="James"),
]
lebron_view = CDSView(source=player_gm_stats, filters=lebron_filters)
durant_filters = [
GroupFilter(column_name="playFNm", group="Kevin"),
GroupFilter(column_name="playLNm", group="Durant"),
]
durant_view = CDSView(source=player_gm_stats, filters=durant_filters)
# Consolidate the common keyword arguments in dicts
common_figure_kwargs = {
"plot_width": 400,
"x_axis_label": "Points",
"toolbar_location": None,
}
common_circle_kwargs = {
"x": "playPTS",
"y": "playTRB",
"source": player_gm_stats,
"size": 12,
"alpha": 0.7,
}
common_lebron_kwargs = {
"view": lebron_view,
"color": "#002859",
"legend": "LeBron James",
}
common_durant_kwargs = {
"view": durant_view,
"color": "#FFC324",
"legend": "Kevin Durant",
}
# Create the two figures and draw the data
hide_fig = figure(
**common_figure_kwargs,
title="Click Legend to HIDE Data",
y_axis_label="Rebounds",
)
hide_fig.circle(**common_circle_kwargs, **common_lebron_kwargs)
hide_fig.circle(**common_circle_kwargs, **common_durant_kwargs)
mute_fig = figure(**common_figure_kwargs, title="Click Legend to MUTE Data")
mute_fig.circle(
**common_circle_kwargs, **common_lebron_kwargs, muted_alpha=0.1
)
mute_fig.circle(
**common_circle_kwargs, **common_durant_kwargs, muted_alpha=0.1
)
# Add interactivity to the legend
hide_fig.legend.click_policy = "hide"
mute_fig.legend.click_policy = "mute"
# Visualize
show(row(hide_fig, mute_fig))