{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "Basic Interactor Demo\n", "---------------------\n", "\n", "This demo shows off an interactive visualization using [Bokeh](http://bokeh.pydata.org) for plotting, and Ipython interactors for widgets. The demo runs entirely inside the Ipython notebook, with no Bokeh server required.\n", "\n", "The dropdown offers a choice of trig functions to plot, and the sliders control the frequency, amplitude, and phase. \n", "\n", "To run, click on, `Cell->Run All` in the top menu, then scroll to the bottom and move the sliders. " ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "from ipywidgets import interact\n", "import numpy as np\n", "\n", "from bokeh.io import push_notebook\n", "from bokeh.plotting import figure, show, output_notebook" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "x = np.linspace(0, 2*np.pi, 2000)\n", "y = np.sin(x)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "output_notebook()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "p = figure(title=\"simple line example\", plot_height=300, plot_width=600, y_range=(-5,5))\n", "r = p.line(x, y, color=\"#2222aa\", line_width=3)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "def update(f, w=1, A=1, phi=0):\n", " if f == \"sin\": func = np.sin\n", " elif f == \"cos\": func = np.cos\n", " elif f == \"tan\": func = np.tan\n", " r.data_source.data['y'] = A * func(w * x + phi)\n", " push_notebook()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "show(p)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "interact(update, f=[\"sin\", \"cos\", \"tan\"], w=(0,100), A=(1,5), phi=(0, 20, 0.1))" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.4.3" } }, "nbformat": 4, "nbformat_minor": 0 }