Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from bokeh.models import CustomJS, ColumnDataSource, MultiSelect, CheckboxGroup, Column
- from bokeh.plotting import figure, show
- import pandas as pd
- data = dict(letter = ['A','A','B','C','B','B','A','C','C','B'],
- x = [1, 2, 1, 2, 3, 2, 2, 3, 2, 3],
- y = ['10','20','10','30','10','40','10','30','10','40'])
- data = pd.DataFrame(data)
- data_source = ColumnDataSource(data)
- source = ColumnDataSource(dict(x = [], y = []))
- plot = figure()
- plot.circle('x', 'y', line_width = 2, source = source)
- callback = CustomJS(args = {'source': source, 'data_source': data_source},
- code = """
- var data = data_source.data;
- var s_data = source.data;
- var letter = data['letter'];
- var select_vals = cb_obj.value;
- var x_data = data['x'];
- var y_data = data['y'];
- var x = s_data['x'];
- x.length = 0;
- var y = s_data['y'];
- y.length = 0;
- for (var i = 0; i < x_data.length; i++) {
- if (select_vals.indexOf(letter[i]) >= 0) {
- x.push(x_data[i]);
- y.push(y_data[i]);
- }
- }
- source.change.emit();
- """)
- multiselect = MultiSelect(title = 'Choose', value = [], options = ['A', 'B', 'C'])
- multiselect.js_on_change('value', callback)
- # Toggle reference curves on/off
- ## Dummy data for plotting lines testing
- x = list(range(10))
- A = [ a**1.5 for a in x]
- B = [ a**1.6 for a in x]
- C = [ a**1.7 for a in x]
- curve_A = plot.line(x, A, color='red')
- curve_B = plot.line(x, B, color='red')
- curve_C = plot.line(x, C, color='red')
- curve_A.visible = False
- curve_B.visible = False
- curve_C.visible = False
- curve_checkbox = CheckboxGroup(labels=["Add reference curve",], active=[])
- curve_checkbox.callback = CustomJS(args=dict(A=A, B=B, C=C, source=source, curve_checkbox=curve_checkbox),
- code="""
- curve_A.visible = 0 in checkbox.active;
- """)
- layout = Column(multiselect, curve_checkbox, plot)
- show(layout)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement