Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import numpy as np
- from bokeh.io import hplot, vplot, VBoxForm, HBox, vform, curstate
- from bokeh.plotting import figure
- from bokeh.models.widgets import Slider, Select, Toggle
- r1 = None
- r2 = None
- all_plots = []
- x = np.linspace(0, 2*np.pi, 2000)
- y = np.sin(x)
- f = None
- w = None
- A = None
- phi = None
- def get_display_graphs():
- global r1, r2
- p1 = figure(title="simple line example", plot_height=300, plot_width=600,
- y_range=(-5,5), toolbar_location=None)
- r1 = p1.line(x, y, color="#2222aa", line_width=3)
- p2 = figure(title="simple line example -2", plot_height=300, plot_width=600,
- y_range=(-5,5), toolbar_location=None)
- r2 = p2.line(x, y, color="#FF0000", line_width=3)
- p3 = vplot(p1, p2)
- return p3
- trig_functions = {
- 'sin': np.sin,
- 'cos': np.cos,
- 'tan': np.tan,
- }
- def update(attrname, old, new):
- func = trig_functions.get(f.value)
- r1.data_source.data['y'] = A.value * func(w.value * x + phi.value)
- r2.data_source.data['y'] = 1 + A.value * func(w.value * x + phi.value)
- def get_display_controls():
- global f, w, A, phi
- f = Select(title="fn", value="sin",
- options=list(trig_functions.keys()))
- w = Slider(title="W", start=0, end=100, value=50, step=1)
- A = Slider(title="A", start=1, end=5, value=1, step=1)
- phi = Slider(title="phi", start=0, end=20, value=5, step=0.1)
- controls = [f, w, A, phi]
- for control in controls:
- control.on_change('value', update)
- inputs = HBox(VBoxForm(*controls), width=300)
- return inputs
- def display_all():
- inputs = get_display_controls()
- graphs = get_display_graphs()
- all_plots.append(inputs)
- all_plots.append(graphs)
- doc = curstate().document
- doc.add_root(hplot(inputs, graphs))
- def on_button_clicked(b):
- print("In button clicked")
- display_all()
- button = Toggle()
- button.on_click(on_button_clicked)
- all_plots.append(vform(button))
- mainplot = vplot(*all_plots)
Add Comment
Please, Sign In to add comment