Back to Bokeh

Jupyter Interactors

examples/output/jupyter/push_notebook/Jupyter Interactors.ipynb

3.10.0.dev41.2 KB
Original Source

Basic Interactor Demo

This demo shows off an interactive visualization using Bokeh for plotting, and Ipython interactors for widgets. The demo runs entirely inside the Ipython notebook, with no Bokeh server required.

The dropdown offers a choice of trig functions to plot, and the sliders control the frequency, amplitude, and phase.

To run, click on, Cell->Run All in the top menu, then scroll to the bottom and move the sliders.

python
from ipywidgets import interact
import numpy as np

from bokeh.io import push_notebook, show, output_notebook
from bokeh.plotting import figure
output_notebook()
python
x = np.linspace(0, 2*np.pi, 2000)
y = np.sin(x)
python
p = figure(title="simple line example", height=300, width=600, y_range=(-5,5),
           background_fill_color='#efefef')
r = p.line(x, y, color="#8888cc", line_width=1.5, alpha=0.8)
python
def update(f, w=1, A=1, phi=0):
    if   f == "sin": func = np.sin
    elif f == "cos": func = np.cos
    r.data_source.data['y'] = A * func(w * x + phi)
    push_notebook()
python
show(p, notebook_handle=True)
python
interact(update, f=["sin", "cos"], w=(0,50), A=(1,10), phi=(0, 20, 0.1))