|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": {}, |
| 6 | + "source": [ |
| 7 | + "In this app we:\n", |
| 8 | + "* Plot the gaussian density for a specific $\\mu$ and $\\sigma$\n", |
| 9 | + "* Use the FloatSlider widget in ipywidgets to represent $\\mu$ and $\\sigma$ values\n", |
| 10 | + "* Stack the density plot along with the sliders into a nice layout using HBox and VBox layout objects available in ipywidgets\n", |
| 11 | + "* Link the sliders to the plot so that the plot gets updated when the values of $\\mu$ and $\\sigma$ change\n", |
| 12 | + "\n", |
| 13 | + "Find the code [here](https://github.com/pbugnion/voila-gallery/blob/master/gaussian-density/index.ipynb).\n", |
| 14 | + "\n", |
| 15 | + "This example is taken from [ChakriCherukuri/mlviz](https://github.com/ChakriCherukuri/mlviz)." |
| 16 | + ] |
| 17 | + }, |
| 18 | + { |
| 19 | + "cell_type": "code", |
| 20 | + "execution_count": null, |
| 21 | + "metadata": {}, |
| 22 | + "outputs": [], |
| 23 | + "source": [ |
| 24 | + "import numpy as np\n", |
| 25 | + "from scipy.stats import norm\n", |
| 26 | + "\n", |
| 27 | + "from ipywidgets import FloatSlider, HBox, VBox\n", |
| 28 | + "import bqplot.pyplot as plt" |
| 29 | + ] |
| 30 | + }, |
| 31 | + { |
| 32 | + "cell_type": "code", |
| 33 | + "execution_count": null, |
| 34 | + "metadata": {}, |
| 35 | + "outputs": [], |
| 36 | + "source": [ |
| 37 | + "x = np.linspace(-10, 10, 200)\n", |
| 38 | + "y = norm.pdf(x)\n", |
| 39 | + "\n", |
| 40 | + "# plot the gaussian density\n", |
| 41 | + "title_tmpl = 'Gaussian Density (mu = {} and sigma = {})'\n", |
| 42 | + "pdf_fig = plt.figure(title=title_tmpl.format(0, 1))\n", |
| 43 | + "pdf_line = plt.plot(x, y, 'm', stroke_width=3)" |
| 44 | + ] |
| 45 | + }, |
| 46 | + { |
| 47 | + "cell_type": "code", |
| 48 | + "execution_count": null, |
| 49 | + "metadata": {}, |
| 50 | + "outputs": [], |
| 51 | + "source": [ |
| 52 | + "# use two sliders to represent mu and sigma\n", |
| 53 | + "mu_slider = FloatSlider(description='mu', value=0, min=-5, max=5, step=.1)\n", |
| 54 | + "sigma_slider = FloatSlider(description='sigma', value=1, min=0.1, max=5, step=.1)\n", |
| 55 | + "\n", |
| 56 | + "slider_layout = HBox([mu_slider, sigma_slider])" |
| 57 | + ] |
| 58 | + }, |
| 59 | + { |
| 60 | + "cell_type": "code", |
| 61 | + "execution_count": null, |
| 62 | + "metadata": {}, |
| 63 | + "outputs": [], |
| 64 | + "source": [ |
| 65 | + "def update_density(change):\n", |
| 66 | + " new_mu = mu_slider.value\n", |
| 67 | + " new_sigma = sigma_slider.value\n", |
| 68 | + " # update the y attribute of the plot with the new pdf\n", |
| 69 | + " # computed using new mu and sigma values\n", |
| 70 | + " pdf_line.y = norm.pdf(x, new_mu, new_sigma)\n", |
| 71 | + " \n", |
| 72 | + " # also update the fig title\n", |
| 73 | + " pdf_fig.title = title_tmpl.format(new_mu, new_sigma)\n", |
| 74 | + "\n", |
| 75 | + "# register the above callback with the 'value' trait of the sliders\n", |
| 76 | + "mu_slider.observe(update_density, 'value')\n", |
| 77 | + "sigma_slider.observe(update_density, 'value')" |
| 78 | + ] |
| 79 | + }, |
| 80 | + { |
| 81 | + "cell_type": "code", |
| 82 | + "execution_count": null, |
| 83 | + "metadata": {}, |
| 84 | + "outputs": [], |
| 85 | + "source": [ |
| 86 | + "# now put all the widgets together into a simple dashboard\n", |
| 87 | + "# the plot should update now when the slider values are updated!\n", |
| 88 | + "final_layout = VBox([pdf_fig, slider_layout])\n", |
| 89 | + "final_layout" |
| 90 | + ] |
| 91 | + } |
| 92 | + ], |
| 93 | + "metadata": { |
| 94 | + "kernelspec": { |
| 95 | + "display_name": "Python 3 (ipykernel)", |
| 96 | + "language": "python", |
| 97 | + "name": "python3" |
| 98 | + }, |
| 99 | + "language_info": { |
| 100 | + "codemirror_mode": { |
| 101 | + "name": "ipython", |
| 102 | + "version": 3 |
| 103 | + }, |
| 104 | + "file_extension": ".py", |
| 105 | + "mimetype": "text/x-python", |
| 106 | + "name": "python", |
| 107 | + "nbconvert_exporter": "python", |
| 108 | + "pygments_lexer": "ipython3", |
| 109 | + "version": "3.9.16" |
| 110 | + }, |
| 111 | + "rsconnect": { |
| 112 | + "previousServerId": null, |
| 113 | + "servers": {}, |
| 114 | + "version": 1 |
| 115 | + } |
| 116 | + }, |
| 117 | + "nbformat": 4, |
| 118 | + "nbformat_minor": 2 |
| 119 | +} |
0 commit comments