{
"cells": [
{
"cell_type": "markdown",
"id": "35cfb208",
"metadata": {},
"source": [
"# Plot graph using `bokeh`\n",
"\n",
"#### Topics covered in this example\n",
"* Installing python libraries on the EMR cluster.\n",
"* Use Bokeh plotting library to plot trignometric functions: sin, cos and tan functions. "
]
},
{
"cell_type": "markdown",
"id": "26237079",
"metadata": {},
"source": [
"***\n",
"\n",
"## Introduction\n",
"This is an example from bokeh tutorial. In this example, we are going to use Bokeh to plot an interactive graph. We will plot different trignometric functions, namely sin, cos and tan. To interactive with the graph, we will use a dropdown that offers a choice to select a function and the sliders to control the frequency, amplitude, and phase.\n",
"\n",
"***"
]
},
{
"cell_type": "markdown",
"id": "81f6b2ab",
"metadata": {},
"source": [
"Install dependency `bokeh` using `%pip` cell magic.\n",
"\n",
"`%pip install` is same as `!/emr/notebook-env/bin/pip install` and are installed in `/home/emr-notebook/`.\n",
"\n",
"After installation, these libraries are available to any user running an EMR notebook attached to the cluster. Python libraries installed this way are available only to processes running on the master node. The libraries are not installed on core or task nodes and are not available to executors running on those nodes."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "c13d9f55",
"metadata": {},
"outputs": [],
"source": [
"%pip install matplotlib"
]
},
{
"cell_type": "markdown",
"id": "d38052f4",
"metadata": {},
"source": [
"import dependencies"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "976fa391",
"metadata": {},
"outputs": [],
"source": [
"from ipywidgets import interact\n",
"import numpy as np\n",
"\n",
"from bokeh.io import push_notebook, show, output_notebook\n",
"from bokeh.plotting import figure\n",
"output_notebook()"
]
},
{
"cell_type": "markdown",
"id": "281a2bb0",
"metadata": {},
"source": [
"Generate x and y axes co-ordinates for sin function.\n",
"For x axis, take an array of evenly spaced numbers and calculate corresponding y axis co-ordinate."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "0624f7b4",
"metadata": {},
"outputs": [],
"source": [
"x = np.linspace(0, 2*np.pi, 2000)\n",
"y = np.sin(x)"
]
},
{
"cell_type": "markdown",
"id": "eb01a95c",
"metadata": {},
"source": [
"Define a graph and draw a line with sin as default function"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "201d45b4",
"metadata": {},
"outputs": [],
"source": [
"p = figure(title=\"simple line example\", plot_height=300, plot_width=600, y_range=(-5,5),\n",
" background_fill_color=\"#efefef\")\n",
"r = p.line(x, y, color=\"#8888cc\", line_width=1.5, alpha=0.8)"
]
},
{
"cell_type": "markdown",
"id": "52fa47ba",
"metadata": {},
"source": [
"Define an update function that will be callable to interactors to update the graph with different trignometic functions."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "76215a3b",
"metadata": {},
"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": "markdown",
"id": "7fbe402e",
"metadata": {},
"source": [
"Render the graph"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "ccf184ad",
"metadata": {},
"outputs": [],
"source": [
"show(p, notebook_handle=True)"
]
},
{
"cell_type": "markdown",
"id": "75216e25",
"metadata": {},
"source": [
"Interact with the graph: dropdown to select a trignometric functions and sliders to change the value of frequency, amplitude, and phase"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "acf00693",
"metadata": {},
"outputs": [],
"source": [
"interact(update, f=[\"sin\", \"cos\"], w=(0,50), A=(1,10), phi=(0, 20, 0.1))"
]
}
],
"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.7.4"
}
},
"nbformat": 4,
"nbformat_minor": 5
}