Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from itertools import product
- import pandas as pd
- from bokeh.io import show, output_file
- from bokeh.layouts import column
- from bokeh.models import CustomJS
- from bokeh.models.sources import ColumnDataSource
- from bokeh.models.widgets import Slider
- from bokeh.plotting import figure
- N = 30
- INITIAL_STEP = 0
- data = []
- for r, c in product(range(N), range(N)):
- step = max(r, c)
- data.append(dict(x=c, y=r,
- infected_by_x=(c - 1 if c > 0 else c),
- infected_by_y=(r - 1 if r > 0 else r),
- infected_step=step,
- color='blue' if step > INITIAL_STEP else 'red'))
- data = pd.DataFrame(data)
- ds = ColumnDataSource(data)
- line_data = []
- for row in data.itertuples():
- step = max(row.x, row.y)
- line_data.append(dict(xs=[row.infected_by_x, row.x],
- ys=[row.infected_by_y, row.y],
- infected_step=row.infected_step,
- alpha=0 if step > INITIAL_STEP else 1))
- line_data = pd.DataFrame(line_data)
- line_ds = ColumnDataSource(line_data)
- slider = Slider(start=0, end=N - 1, value=INITIAL_STEP)
- slider.js_on_change('value', CustomJS(args=dict(dots_ds=ds, lines_ds=line_ds),
- code="""
- var update_status = function(ds, field, healthy, infected) {
- for (var di = 0; di < ds.data[field].length; ++di) {
- ds.data[field][di] = (ds.data['infected_step'][di] > cb_obj.value) ? healthy : infected;
- }
- ds.change.emit();
- };
- update_status(dots_ds, 'color', 'blue', 'red');
- update_status(lines_ds, 'alpha', 0, 1);
- """))
- f = figure()
- f.circle(x='x', y='y', color='color', source=ds)
- f.multi_line(xs='xs', ys='ys', color='red', alpha='alpha', source=line_ds)
- output_file('test.html')
- show(column(f, slider))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement