bokeh.ipynb 16.1 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# `bokeh`\n",
    "\n",
    "[`bokeh`](https://docs.bokeh.org/en/latest/index.html) is a Python library for creating interactive visualizations for modern web browsers. `bokeh` allows you to create these interactive web-based plots without having to code in javascript.\n",
    "\n",
    "`bokeh` has excellent documentation: https://docs.bokeh.org/en/latest/index.html\n",
    "\n",
    "This notebook is not intended to instruct you how to use `bokeh`.  Instead it pulls together interesting examples from the `bokeh` documentation into a single notebook to give you a taster of what can be done with `bokeh`.\n",
14
15
16
17
18
19
20
21
22
23
    "\n",
    "## Install `bokeh`\n",
    "\n",
    "`bokeh` is not installed in the `fslpython` environment so you will need to install it to run this notebook. In a terminal run the following command (you will need admin privileges):\n",
    "\n",
    "```\n",
    "sudo $FSLDIR/fslpython/bin/conda install -c conda-forge -n fslpython bokeh\n",
    "```\n",
    "\n",
    "Setup bokeh to work in this notebook:"
24
25
26
27
28
29
30
31
32
33
34
35
36
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from bokeh.plotting import figure, output_file, show\n",
    "from bokeh.io import output_notebook\n",
    "output_notebook()"
   ]
  },
37
38
39
40
41
42
43
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Fetch some sampledata for the examples:"
   ]
  },
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from bokeh import sampledata\n",
    "sampledata.download()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Scatter Plots\n",
    "\n",
    "https://docs.bokeh.org/en/latest/docs/gallery/iris.html"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from bokeh.sampledata.iris import flowers\n",
    "\n",
    "colormap = {'setosa': 'red', 'versicolor': 'green', 'virginica': 'blue'}\n",
    "colors = [colormap[x] for x in flowers['species']]\n",
    "\n",
    "p = figure(title = \"Iris Morphology\")\n",
    "p.xaxis.axis_label = 'Petal Length'\n",
    "p.yaxis.axis_label = 'Petal Width'\n",
    "\n",
    "p.circle(flowers[\"petal_length\"], flowers[\"petal_width\"],\n",
    "         color=colors, fill_alpha=0.2, size=10)\n",
    "\n",
    "# output_file(\"iris.html\", title=\"iris.py example\")\n",
    "\n",
    "show(p)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Line Plots\n",
    "\n",
    "https://docs.bokeh.org/en/latest/docs/gallery/box_annotation.html"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from bokeh.models import BoxAnnotation\n",
    "from bokeh.sampledata.glucose import data\n",
    "\n",
    "TOOLS = \"pan,wheel_zoom,box_zoom,reset,save\"\n",
    "\n",
    "data = data.loc['2010-10-04':'2010-10-04']\n",
    "\n",
    "p = figure(x_axis_type=\"datetime\", tools=TOOLS, title=\"Glocose Readings, Oct 4th (Red = Outside Range)\")\n",
    "p.background_fill_color = \"#efefef\"\n",
    "p.xgrid.grid_line_color=None\n",
    "p.xaxis.axis_label = 'Time'\n",
    "p.yaxis.axis_label = 'Value'\n",
    "\n",
    "p.line(data.index, data.glucose, line_color='grey')\n",
    "p.circle(data.index, data.glucose, color='grey', size=1)\n",
    "\n",
    "p.add_layout(BoxAnnotation(top=80, fill_alpha=0.1, fill_color='red', line_color='red'))\n",
    "p.add_layout(BoxAnnotation(bottom=180, fill_alpha=0.1, fill_color='red', line_color='red'))\n",
    "\n",
    "show(p)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Bar Charts\n",
    "\n",
    "https://docs.bokeh.org/en/latest/docs/gallery/bar_stacked.html"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from bokeh.palettes import Spectral5\n",
    "from bokeh.sampledata.autompg import autompg_clean as df\n",
    "from bokeh.transform import factor_cmap\n",
    "\n",
    "df.cyl = df.cyl.astype(str)\n",
    "df.yr = df.yr.astype(str)\n",
    "\n",
    "group = df.groupby(['cyl', 'mfr'])\n",
    "\n",
    "index_cmap = factor_cmap('cyl_mfr', palette=Spectral5, factors=sorted(df.cyl.unique()), end=1)\n",
    "\n",
    "p = figure(plot_width=800, plot_height=500, title=\"Mean MPG by # Cylinders and Manufacturer\",\n",
    "           x_range=group, toolbar_location=None, tooltips=[(\"MPG\", \"@mpg_mean\"), (\"Cyl, Mfr\", \"@cyl_mfr\")])\n",
    "\n",
    "p.vbar(x='cyl_mfr', top='mpg_mean', width=1, source=group,\n",
    "       line_color=\"white\", fill_color=index_cmap, )\n",
    "\n",
    "p.y_range.start = 0\n",
    "p.x_range.range_padding = 0.05\n",
    "p.xgrid.grid_line_color = None\n",
    "p.xaxis.axis_label = \"Manufacturer grouped by # Cylinders\"\n",
    "p.xaxis.major_label_orientation = 1.2\n",
    "p.outline_line_color = None\n",
    "\n",
    "show(p)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Distribution Plots\n",
    "\n",
    "https://docs.bokeh.org/en/latest/docs/gallery/histogram.html"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import scipy.special\n",
    "\n",
    "from bokeh.layouts import gridplot\n",
    "\n",
    "\n",
    "def make_plot(title, hist, edges, x, pdf, cdf):\n",
    "    p = figure(title=title, tools='', background_fill_color=\"#fafafa\")\n",
    "    p.quad(top=hist, bottom=0, left=edges[:-1], right=edges[1:],\n",
    "           fill_color=\"navy\", line_color=\"white\", alpha=0.5)\n",
    "    p.line(x, pdf, line_color=\"#ff8888\", line_width=4, alpha=0.7, legend_label=\"PDF\")\n",
    "    p.line(x, cdf, line_color=\"orange\", line_width=2, alpha=0.7, legend_label=\"CDF\")\n",
    "\n",
    "    p.y_range.start = 0\n",
    "    p.legend.location = \"center_right\"\n",
    "    p.legend.background_fill_color = \"#fefefe\"\n",
    "    p.xaxis.axis_label = 'x'\n",
    "    p.yaxis.axis_label = 'Pr(x)'\n",
    "    p.grid.grid_line_color=\"white\"\n",
    "    return p\n",
    "\n",
    "# Normal Distribution\n",
    "\n",
    "mu, sigma = 0, 0.5\n",
    "\n",
    "measured = np.random.normal(mu, sigma, 1000)\n",
    "hist, edges = np.histogram(measured, density=True, bins=50)\n",
    "\n",
    "x = np.linspace(-2, 2, 1000)\n",
    "pdf = 1/(sigma * np.sqrt(2*np.pi)) * np.exp(-(x-mu)**2 / (2*sigma**2))\n",
    "cdf = (1+scipy.special.erf((x-mu)/np.sqrt(2*sigma**2)))/2\n",
    "\n",
    "p1 = make_plot(\"Normal Distribution (μ=0, σ=0.5)\", hist, edges, x, pdf, cdf)\n",
    "\n",
    "# Log-Normal Distribution\n",
    "\n",
    "mu, sigma = 0, 0.5\n",
    "\n",
    "measured = np.random.lognormal(mu, sigma, 1000)\n",
    "hist, edges = np.histogram(measured, density=True, bins=50)\n",
    "\n",
    "x = np.linspace(0.0001, 8.0, 1000)\n",
    "pdf = 1/(x* sigma * np.sqrt(2*np.pi)) * np.exp(-(np.log(x)-mu)**2 / (2*sigma**2))\n",
    "cdf = (1+scipy.special.erf((np.log(x)-mu)/(np.sqrt(2)*sigma)))/2\n",
    "\n",
    "p2 = make_plot(\"Log Normal Distribution (μ=0, σ=0.5)\", hist, edges, x, pdf, cdf)\n",
    "\n",
    "# Gamma Distribution\n",
    "\n",
    "k, theta = 7.5, 1.0\n",
    "\n",
    "measured = np.random.gamma(k, theta, 1000)\n",
    "hist, edges = np.histogram(measured, density=True, bins=50)\n",
    "\n",
    "x = np.linspace(0.0001, 20.0, 1000)\n",
    "pdf = x**(k-1) * np.exp(-x/theta) / (theta**k * scipy.special.gamma(k))\n",
    "cdf = scipy.special.gammainc(k, x/theta)\n",
    "\n",
    "p3 = make_plot(\"Gamma Distribution (k=7.5, θ=1)\", hist, edges, x, pdf, cdf)\n",
    "\n",
    "# Weibull Distribution\n",
    "\n",
    "lam, k = 1, 1.25\n",
    "measured = lam*(-np.log(np.random.uniform(0, 1, 1000)))**(1/k)\n",
    "hist, edges = np.histogram(measured, density=True, bins=50)\n",
    "\n",
    "x = np.linspace(0.0001, 8, 1000)\n",
    "pdf = (k/lam)*(x/lam)**(k-1) * np.exp(-(x/lam)**k)\n",
    "cdf = 1 - np.exp(-(x/lam)**k)\n",
    "\n",
    "p4 = make_plot(\"Weibull Distribution (λ=1, k=1.25)\", hist, edges, x, pdf, cdf)\n",
    "\n",
    "show(gridplot([p1,p2,p3,p4], ncols=2, plot_width=400, plot_height=400, toolbar_location=None))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Boxplot\n",
    "\n",
    "https://docs.bokeh.org/en/latest/docs/gallery/boxplot.html"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "\n",
    "# generate some synthetic time series for six different categories\n",
    "cats = list(\"abcdef\")\n",
    "yy = np.random.randn(2000)\n",
    "g = np.random.choice(cats, 2000)\n",
    "for i, l in enumerate(cats):\n",
    "    yy[g == l] += i // 2\n",
    "df = pd.DataFrame(dict(score=yy, group=g))\n",
    "\n",
    "# find the quartiles and IQR for each category\n",
    "groups = df.groupby('group')\n",
    "q1 = groups.quantile(q=0.25)\n",
    "q2 = groups.quantile(q=0.5)\n",
    "q3 = groups.quantile(q=0.75)\n",
    "iqr = q3 - q1\n",
    "upper = q3 + 1.5*iqr\n",
    "lower = q1 - 1.5*iqr\n",
    "\n",
    "# find the outliers for each category\n",
    "def outliers(group):\n",
    "    cat = group.name\n",
    "    return group[(group.score > upper.loc[cat]['score']) | (group.score < lower.loc[cat]['score'])]['score']\n",
    "out = groups.apply(outliers).dropna()\n",
    "\n",
    "# prepare outlier data for plotting, we need coordinates for every outlier.\n",
    "if not out.empty:\n",
    "    outx = list(out.index.get_level_values(0))\n",
    "    outy = list(out.values)\n",
    "\n",
    "p = figure(tools=\"\", background_fill_color=\"#efefef\", x_range=cats, toolbar_location=None)\n",
    "\n",
    "# if no outliers, shrink lengths of stems to be no longer than the minimums or maximums\n",
    "qmin = groups.quantile(q=0.00)\n",
    "qmax = groups.quantile(q=1.00)\n",
    "upper.score = [min([x,y]) for (x,y) in zip(list(qmax.loc[:,'score']),upper.score)]\n",
    "lower.score = [max([x,y]) for (x,y) in zip(list(qmin.loc[:,'score']),lower.score)]\n",
    "\n",
    "# stems\n",
    "p.segment(cats, upper.score, cats, q3.score, line_color=\"black\")\n",
    "p.segment(cats, lower.score, cats, q1.score, line_color=\"black\")\n",
    "\n",
    "# boxes\n",
    "p.vbar(cats, 0.7, q2.score, q3.score, fill_color=\"#E08E79\", line_color=\"black\")\n",
    "p.vbar(cats, 0.7, q1.score, q2.score, fill_color=\"#3B8686\", line_color=\"black\")\n",
    "\n",
    "# whiskers (almost-0 height rects simpler than segments)\n",
    "p.rect(cats, lower.score, 0.2, 0.01, line_color=\"black\")\n",
    "p.rect(cats, upper.score, 0.2, 0.01, line_color=\"black\")\n",
    "\n",
    "# outliers\n",
    "if not out.empty:\n",
    "    p.circle(outx, outy, size=6, color=\"#F38630\", fill_alpha=0.6)\n",
    "\n",
    "p.xgrid.grid_line_color = None\n",
    "p.ygrid.grid_line_color = \"white\"\n",
    "p.grid.grid_line_width = 2\n",
    "p.xaxis.major_label_text_font_size=\"16px\"\n",
    "\n",
    "show(p)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Connectivity Matrix\n",
    "\n",
    "https://docs.bokeh.org/en/latest/docs/gallery/les_mis.html"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "from bokeh.sampledata.les_mis import data\n",
    "\n",
    "nodes = data['nodes']\n",
    "names = [node['name'] for node in sorted(data['nodes'], key=lambda x: x['group'])]\n",
    "\n",
    "N = len(nodes)\n",
    "counts = np.zeros((N, N))\n",
    "for link in data['links']:\n",
    "    counts[link['source'], link['target']] = link['value']\n",
    "    counts[link['target'], link['source']] = link['value']\n",
    "\n",
    "colormap = [\"#444444\", \"#a6cee3\", \"#1f78b4\", \"#b2df8a\", \"#33a02c\", \"#fb9a99\",\n",
    "            \"#e31a1c\", \"#fdbf6f\", \"#ff7f00\", \"#cab2d6\", \"#6a3d9a\"]\n",
    "\n",
    "xname = []\n",
    "yname = []\n",
    "color = []\n",
    "alpha = []\n",
    "for i, node1 in enumerate(nodes):\n",
    "    for j, node2 in enumerate(nodes):\n",
    "        xname.append(node1['name'])\n",
    "        yname.append(node2['name'])\n",
    "\n",
    "        alpha.append(min(counts[i,j]/4.0, 0.9) + 0.1)\n",
    "\n",
    "        if node1['group'] == node2['group']:\n",
    "            color.append(colormap[node1['group']])\n",
    "        else:\n",
    "            color.append('lightgrey')\n",
    "\n",
    "data=dict(\n",
    "    xname=xname,\n",
    "    yname=yname,\n",
    "    colors=color,\n",
    "    alphas=alpha,\n",
    "    count=counts.flatten(),\n",
    ")\n",
    "\n",
    "p = figure(title=\"Les Mis Occurrences\",\n",
    "           x_axis_location=\"above\", tools=\"hover,save\",\n",
    "           x_range=list(reversed(names)), y_range=names,\n",
    "           tooltips = [('names', '@yname, @xname'), ('count', '@count')])\n",
    "\n",
    "p.plot_width = 800\n",
    "p.plot_height = 800\n",
    "p.grid.grid_line_color = None\n",
    "p.axis.axis_line_color = None\n",
    "p.axis.major_tick_line_color = None\n",
    "p.axis.major_label_text_font_size = \"7px\"\n",
    "p.axis.major_label_standoff = 0\n",
    "p.xaxis.major_label_orientation = np.pi/3\n",
    "\n",
    "p.rect('xname', 'yname', 0.9, 0.9, source=data,\n",
    "       color='colors', alpha='alphas', line_color=None,\n",
    "       hover_line_color='black', hover_color='colors')\n",
    "\n",
    "show(p) # show the plot"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Sliders\n",
    "\n",
    "https://docs.bokeh.org/en/latest/docs/gallery/slider.html"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "\n",
    "from bokeh.layouts import column, row\n",
    "from bokeh.models import CustomJS, Slider\n",
    "from bokeh.plotting import ColumnDataSource\n",
    "\n",
    "x = np.linspace(0, 10, 500)\n",
    "y = np.sin(x)\n",
    "\n",
    "source = ColumnDataSource(data=dict(x=x, y=y))\n",
    "\n",
    "plot = figure(y_range=(-10, 10), plot_width=400, plot_height=400)\n",
    "\n",
    "plot.line('x', 'y', source=source, line_width=3, line_alpha=0.6)\n",
    "\n",
    "amp_slider = Slider(start=0.1, end=10, value=1, step=.1, title=\"Amplitude\")\n",
    "freq_slider = Slider(start=0.1, end=10, value=1, step=.1, title=\"Frequency\")\n",
    "phase_slider = Slider(start=0, end=6.4, value=0, step=.1, title=\"Phase\")\n",
    "offset_slider = Slider(start=-5, end=5, value=0, step=.1, title=\"Offset\")\n",
    "\n",
    "callback = CustomJS(args=dict(source=source, amp=amp_slider, freq=freq_slider, phase=phase_slider, offset=offset_slider),\n",
    "                    code=\"\"\"\n",
    "    const data = source.data;\n",
    "    const A = amp.value;\n",
    "    const k = freq.value;\n",
    "    const phi = phase.value;\n",
    "    const B = offset.value;\n",
    "    const x = data['x']\n",
    "    const y = data['y']\n",
    "    for (var i = 0; i < x.length; i++) {\n",
    "        y[i] = B + A*Math.sin(k*x[i]+phi);\n",
    "    }\n",
    "    source.change.emit();\n",
    "\"\"\")\n",
    "\n",
    "amp_slider.js_on_change('value', callback)\n",
    "freq_slider.js_on_change('value', callback)\n",
    "phase_slider.js_on_change('value', callback)\n",
    "offset_slider.js_on_change('value', callback)\n",
    "\n",
    "layout = row(\n",
    "    plot,\n",
    "    column(amp_slider, freq_slider, phase_slider, offset_slider),\n",
    ")\n",
    "\n",
    "show(layout)"
   ]
  }
 ],
 "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.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}