|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "code", |
| 5 | + "execution_count": null, |
| 6 | + "metadata": {}, |
| 7 | + "outputs": [], |
| 8 | + "source": [ |
| 9 | + "#step-1 download the sample data from library,\n", |
| 10 | + "# import bokeh.sampledata\n", |
| 11 | + "# bokeh.sampledata.download()" |
| 12 | + ] |
| 13 | + }, |
| 14 | + { |
| 15 | + "cell_type": "code", |
| 16 | + "execution_count": null, |
| 17 | + "metadata": {}, |
| 18 | + "outputs": [], |
| 19 | + "source": [ |
| 20 | + "from pathlib import Path\n", |
| 21 | + "DATA_PATH = Path('../datasets/chap5_data/')" |
| 22 | + ] |
| 23 | + }, |
| 24 | + { |
| 25 | + "cell_type": "code", |
| 26 | + "execution_count": null, |
| 27 | + "metadata": {}, |
| 28 | + "outputs": [], |
| 29 | + "source": [ |
| 30 | + "#step-2 import required libraries\n", |
| 31 | + "import pandas as pd\n", |
| 32 | + "from bokeh.plotting import figure, output_notebook, show, ColumnDataSource\n", |
| 33 | + "from bokeh.io import push_notebook, show, output_notebook\n", |
| 34 | + "from ipywidgets import interact\n", |
| 35 | + "output_notebook()" |
| 36 | + ] |
| 37 | + }, |
| 38 | + { |
| 39 | + "cell_type": "code", |
| 40 | + "execution_count": null, |
| 41 | + "metadata": {}, |
| 42 | + "outputs": [], |
| 43 | + "source": [ |
| 44 | + "# Step-3 initalize the figure\n", |
| 45 | + "TOOLTIPS = [\n", |
| 46 | + " (\"date\", \"@date\"),\n", |
| 47 | + " (\"value\", \"@close\")\n", |
| 48 | + "]\n", |
| 49 | + "p = figure(title=\"Interactive plot to change line width and color\", plot_width=900,\n", |
| 50 | + " plot_height=400, x_axis_type=\"datetime\", tooltips=TOOLTIPS)\n" |
| 51 | + ] |
| 52 | + }, |
| 53 | + { |
| 54 | + "cell_type": "code", |
| 55 | + "execution_count": null, |
| 56 | + "metadata": {}, |
| 57 | + "outputs": [], |
| 58 | + "source": [ |
| 59 | + "# step-4 helper function to return dataframes.\n", |
| 60 | + "def prepare_data():\n", |
| 61 | + " microsoft_stock = pd.read_csv(DATA_PATH / \"microsoft_stock_ex6.csv\")\n", |
| 62 | + " microsoft_stock[\"date\"] = pd.to_datetime(microsoft_stock[\"date\"])\n", |
| 63 | + " google_stock = pd.read_csv(DATA_PATH / \"google_stock_ex6.csv\")\n", |
| 64 | + " google_stock[\"date\"] = pd.to_datetime(google_stock[\"date\"])\n", |
| 65 | + " \n", |
| 66 | + " return microsoft_stock, google_stock\n" |
| 67 | + ] |
| 68 | + }, |
| 69 | + { |
| 70 | + "cell_type": "code", |
| 71 | + "execution_count": null, |
| 72 | + "metadata": {}, |
| 73 | + "outputs": [], |
| 74 | + "source": [ |
| 75 | + "# step-5 call the helper function to get the dataframes\n", |
| 76 | + "microsoft_stock, google_stock = prepare_data()" |
| 77 | + ] |
| 78 | + }, |
| 79 | + { |
| 80 | + "cell_type": "code", |
| 81 | + "execution_count": null, |
| 82 | + "metadata": {}, |
| 83 | + "outputs": [], |
| 84 | + "source": [ |
| 85 | + "# step-6 Add the lines for both dataframes\n", |
| 86 | + "microsoft_line=p.line(\"date\",\"close\", source=microsoft_stock, line_width=1.5, legend=\"microsoft_stock\")\n", |
| 87 | + "google_line = p.line(\"date\", \"close\", source=google_stock, line_width=1.5, legend=\"google_stock\")" |
| 88 | + ] |
| 89 | + }, |
| 90 | + { |
| 91 | + "cell_type": "code", |
| 92 | + "execution_count": null, |
| 93 | + "metadata": {}, |
| 94 | + "outputs": [], |
| 95 | + "source": [ |
| 96 | + "#custom function define how to interact for user event.\n", |
| 97 | + "def update(color, width=1):\n", |
| 98 | + " google_line.glyph.line_color = color\n", |
| 99 | + " google_line.glyph.line_width = width\n", |
| 100 | + " push_notebook()" |
| 101 | + ] |
| 102 | + }, |
| 103 | + { |
| 104 | + "cell_type": "code", |
| 105 | + "execution_count": null, |
| 106 | + "metadata": {}, |
| 107 | + "outputs": [], |
| 108 | + "source": [ |
| 109 | + "#step-7 plot the required libraries\n", |
| 110 | + "interact(update, color=[\"red\", \"blue\", \"gray\"], width=(1,5))\n", |
| 111 | + "show(p, notebook_handle=True)" |
| 112 | + ] |
| 113 | + }, |
| 114 | + { |
| 115 | + "cell_type": "code", |
| 116 | + "execution_count": null, |
| 117 | + "metadata": {}, |
| 118 | + "outputs": [], |
| 119 | + "source": [] |
| 120 | + }, |
| 121 | + { |
| 122 | + "cell_type": "code", |
| 123 | + "execution_count": null, |
| 124 | + "metadata": {}, |
| 125 | + "outputs": [], |
| 126 | + "source": [] |
| 127 | + } |
| 128 | + ], |
| 129 | + "metadata": { |
| 130 | + "kernelspec": { |
| 131 | + "display_name": "Python 3", |
| 132 | + "language": "python", |
| 133 | + "name": "python3" |
| 134 | + }, |
| 135 | + "language_info": { |
| 136 | + "codemirror_mode": { |
| 137 | + "name": "ipython", |
| 138 | + "version": 3 |
| 139 | + }, |
| 140 | + "file_extension": ".py", |
| 141 | + "mimetype": "text/x-python", |
| 142 | + "name": "python", |
| 143 | + "nbconvert_exporter": "python", |
| 144 | + "pygments_lexer": "ipython3", |
| 145 | + "version": "3.7.3" |
| 146 | + } |
| 147 | + }, |
| 148 | + "nbformat": 4, |
| 149 | + "nbformat_minor": 2 |
| 150 | +} |
0 commit comments