Skip to content

Instantly share code, notes, and snippets.

@eqs
Created April 16, 2016 06:50
Show Gist options
  • Save eqs/5e1c0442f27c7bad470a8db8891a3e09 to your computer and use it in GitHub Desktop.
Save eqs/5e1c0442f27c7bad470a8db8891a3e09 to your computer and use it in GitHub Desktop.
bokeh test
# -*- coding: utf-8 -*-
"""
Created on 04/16/16 14:57:14
Bokehであそぶテスト
@author: eqs
"""
import sys
import numpy as np
from bokeh.plotting import Figure
from bokeh.models import ColumnDataSource, HBox, VBoxForm
from bokeh.models.widgets import Slider
from bokeh.io import curdoc
N = 1000
x = np.linspace(0, 4 * np.pi, N)
y = np.sin(x)
source = ColumnDataSource(data=dict(x=x, y=y))
plot = Figure(plot_height=800, plot_width=800, title='my sine wave',
tools='crosshair,pan,reset,resize,save,wheel_zoom',
x_range=[0, 4*np.pi], y_range=[-5.0, 5.0])
plot.line('x', 'y', source=source, line_width=3, line_alpha=0.6)
offset = Slider(title='offset', value=0.0, start=-5.0, end=5.0, step=0.1)
amplitude = Slider(title='amplitude', value=1.0, start=1.0, end=5.0)
phase = Slider(title='phase', value=0.0, start=-2*np.pi, end=2*np.pi)
freq = Slider(title='freq', value=1.0, start=0.1, end=100.0)
def update_data(attrname, old, new):
a = amplitude.value
b = offset.value
f = freq.value
p = phase.value
x = np.linspace(0, 4 * np.pi, N)
y = a * np.sin(f * x + p) + b
source.data = dict(x=x, y=y)
widgets = [offset, amplitude, phase, freq]
for w in widgets:
w.on_change('value', update_data)
params = VBoxForm(children=widgets)
curdoc().add_root(HBox(children=[params, plot], width=1600))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment