Advertisement
Guest User

Bokeh slider threshold

a guest
Oct 10th, 2017
216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.83 KB | None | 0 0
  1. from itertools import product
  2.  
  3. import pandas as pd
  4. from bokeh.io import show, output_file
  5. from bokeh.layouts import column
  6. from bokeh.models import CustomJS
  7. from bokeh.models.sources import ColumnDataSource
  8. from bokeh.models.widgets import Slider
  9. from bokeh.plotting import figure
  10.  
  11. N = 30
  12. INITIAL_STEP = 0
  13.  
  14. data = []
  15. for r, c in product(range(N), range(N)):
  16.     step = max(r, c)
  17.     data.append(dict(x=c, y=r,
  18.                      infected_by_x=(c - 1 if c > 0 else c),
  19.                      infected_by_y=(r - 1 if r > 0 else r),
  20.                      infected_step=step,
  21.                      color='blue' if step > INITIAL_STEP else 'red'))
  22.  
  23. data = pd.DataFrame(data)
  24. ds = ColumnDataSource(data)
  25.  
  26. line_data = []
  27. for row in data.itertuples():
  28.     step = max(row.x, row.y)
  29.     line_data.append(dict(xs=[row.infected_by_x, row.x],
  30.                           ys=[row.infected_by_y, row.y],
  31.                           infected_step=row.infected_step,
  32.                           alpha=0 if step > INITIAL_STEP else 1))
  33. line_data = pd.DataFrame(line_data)
  34. line_ds = ColumnDataSource(line_data)
  35.  
  36. slider = Slider(start=0, end=N - 1, value=INITIAL_STEP)
  37. slider.js_on_change('value', CustomJS(args=dict(dots_ds=ds, lines_ds=line_ds),
  38.                                       code="""
  39.    var update_status = function(ds, field, healthy, infected) {
  40.        for (var di = 0; di < ds.data[field].length; ++di) {
  41.            ds.data[field][di] = (ds.data['infected_step'][di] > cb_obj.value) ? healthy : infected;
  42.        }
  43.        ds.change.emit();
  44.    };
  45.    update_status(dots_ds, 'color', 'blue', 'red');
  46.    update_status(lines_ds, 'alpha', 0, 1);
  47. """))
  48.  
  49. f = figure()
  50. f.circle(x='x', y='y', color='color', source=ds)
  51. f.multi_line(xs='xs', ys='ys', color='red', alpha='alpha', source=line_ds)
  52.  
  53. output_file('test.html')
  54. show(column(f, slider))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement