Advertisement
Guest User

Untitled

a guest
Jun 24th, 2019
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.86 KB | None | 0 0
  1. from bokeh.models import CustomJS, ColumnDataSource, MultiSelect, CheckboxGroup, Column
  2.  
  3. from bokeh.plotting import figure, show
  4. import pandas as pd
  5.  
  6. data = dict(letter = ['A','A','B','C','B','B','A','C','C','B'],
  7. x = [1, 2, 1, 2, 3, 2, 2, 3, 2, 3],
  8. y = ['10','20','10','30','10','40','10','30','10','40'])
  9. data = pd.DataFrame(data)
  10.  
  11. data_source = ColumnDataSource(data)
  12. source = ColumnDataSource(dict(x = [], y = []))
  13.  
  14. plot = figure()
  15. plot.circle('x', 'y', line_width = 2, source = source)
  16.  
  17. callback = CustomJS(args = {'source': source, 'data_source': data_source},
  18. code = """
  19. var data = data_source.data;
  20. var s_data = source.data;
  21. var letter = data['letter'];
  22. var select_vals = cb_obj.value;
  23. var x_data = data['x'];
  24. var y_data = data['y'];
  25. var x = s_data['x'];
  26. x.length = 0;
  27. var y = s_data['y'];
  28. y.length = 0;
  29. for (var i = 0; i < x_data.length; i++) {
  30. if (select_vals.indexOf(letter[i]) >= 0) {
  31. x.push(x_data[i]);
  32. y.push(y_data[i]);
  33. }
  34. }
  35. source.change.emit();
  36. """)
  37.  
  38. multiselect = MultiSelect(title = 'Choose', value = [], options = ['A', 'B', 'C'])
  39. multiselect.js_on_change('value', callback)
  40.  
  41.  
  42. # Toggle reference curves on/off
  43.  
  44. ## Dummy data for plotting lines testing
  45. x = list(range(10))
  46. A = [ a**1.5 for a in x]
  47. B = [ a**1.6 for a in x]
  48. C = [ a**1.7 for a in x]
  49.  
  50. curve_A = plot.line(x, A, color='red')
  51. curve_B = plot.line(x, B, color='red')
  52. curve_C = plot.line(x, C, color='red')
  53.  
  54. curve_A.visible = False
  55. curve_B.visible = False
  56. curve_C.visible = False
  57.  
  58. curve_checkbox = CheckboxGroup(labels=["Add reference curve",], active=[])
  59. curve_checkbox.callback = CustomJS(args=dict(A=A, B=B, C=C, source=source, curve_checkbox=curve_checkbox),
  60. code="""
  61. curve_A.visible = 0 in checkbox.active;
  62. """)
  63.  
  64. layout = Column(multiselect, curve_checkbox, plot)
  65. show(layout)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement