import random
import pandas as pd
import bokeh.plotting as bk
fruits = ["orange", "plum", "lime"]
data = [(random.choice(fruits), random.randint(0,100), random.randint(0,100)) for i in range(16)]
dframe = pd.DataFrame(data, columns=["fruit", "x", "y"])
dframe
fruit | x | y | |
---|---|---|---|
0 | lime | 32 | 69 |
1 | lime | 71 | 61 |
2 | lime | 32 | 79 |
3 | orange | 33 | 57 |
4 | plum | 45 | 89 |
5 | orange | 75 | 11 |
6 | orange | 84 | 0 |
7 | orange | 44 | 85 |
8 | lime | 46 | 2 |
9 | lime | 75 | 68 |
10 | lime | 73 | 58 |
11 | lime | 21 | 29 |
12 | lime | 98 | 59 |
13 | lime | 91 | 2 |
14 | lime | 98 | 46 |
15 | orange | 23 | 1 |
source = bk.ColumnDataSource(dframe)
p = bk.figure(title="Fruity",
tools="pan,wheel_zoom,box_zoom,reset,resize,hover")
p.circle(x='x', y='y', color='fruit', size=8, source=source)
by_fruit = dframe.groupby('fruit')
def update(fruit):
grouped = by_fruit.get_group(fruit)
# Going through and re-assigning to every column of source.data works,
# but it gets verbose and repetitive as the number of source columns grows.
# Is there a more elegant way to do this?
source.data['x'] = grouped['x']
source.data['y'] = grouped['y']
source.data['fruit'] = grouped['fruit']
source.push_notebook()
bk.show(p)
from IPython.html.widgets import interact
interact(update, fruit=fruits)