Guest User

Simple Plots

a guest
Apr 20th, 2016
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.99 KB | None | 0 0
  1.  
  2. import numpy as np
  3.  
  4. from bokeh.io import hplot, vplot, VBoxForm, HBox, vform, curstate
  5. from bokeh.plotting import figure
  6. from bokeh.models.widgets import Slider, Select,  Toggle
  7.  
  8. r1 = None
  9. r2 = None
  10.  
  11. all_plots = []
  12. x = np.linspace(0, 2*np.pi, 2000)
  13. y = np.sin(x)
  14. f = None
  15. w = None
  16. A = None
  17. phi = None
  18.  
  19.  
  20. def get_display_graphs():
  21.     global r1, r2
  22.     p1 = figure(title="simple line example", plot_height=300, plot_width=600,
  23.         y_range=(-5,5),  toolbar_location=None)
  24.  
  25.     r1 = p1.line(x, y, color="#2222aa", line_width=3)
  26.     p2 = figure(title="simple line example -2", plot_height=300, plot_width=600,
  27.         y_range=(-5,5), toolbar_location=None)
  28.     r2 = p2.line(x, y, color="#FF0000", line_width=3)
  29.     p3 = vplot(p1, p2)
  30.     return p3
  31.  
  32. trig_functions = {
  33.     'sin': np.sin,
  34.     'cos': np.cos,
  35.     'tan': np.tan,
  36. }
  37.  
  38.  
  39. def update(attrname, old, new):
  40.     func = trig_functions.get(f.value)
  41.     r1.data_source.data['y'] = A.value * func(w.value * x + phi.value)
  42.     r2.data_source.data['y'] = 1 + A.value * func(w.value * x + phi.value)
  43.  
  44. def get_display_controls():
  45.     global f, w, A, phi
  46.     f = Select(title="fn", value="sin",
  47.                    options=list(trig_functions.keys()))
  48.     w = Slider(title="W", start=0, end=100, value=50, step=1)
  49.     A = Slider(title="A", start=1, end=5, value=1, step=1)
  50.     phi = Slider(title="phi", start=0, end=20, value=5, step=0.1)
  51.     controls = [f, w, A, phi]
  52.     for control in controls:
  53.         control.on_change('value', update)
  54.     inputs = HBox(VBoxForm(*controls), width=300)
  55.     return inputs
  56.  
  57. def display_all():
  58.     inputs = get_display_controls()
  59.     graphs = get_display_graphs()
  60.     all_plots.append(inputs)
  61.     all_plots.append(graphs)
  62.     doc = curstate().document
  63.     doc.add_root(hplot(inputs, graphs))
  64.  
  65.  
  66. def on_button_clicked(b):
  67.     print("In button clicked")
  68.     display_all()
  69.  
  70. button = Toggle()
  71. button.on_click(on_button_clicked)
  72.  
  73.  
  74. all_plots.append(vform(button))
  75. mainplot = vplot(*all_plots)
Add Comment
Please, Sign In to add comment