Debian Codegraph @

bqplot/examples/Tutorials/Gaussian Density.ipynb

raw
1 {
2 "cells": [
3 {
4 "cell_type": "markdown",
5 "metadata": {},
6 "source": [
7 "In this tutorial notebook we'll learn how to:\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 "* Layout the density plot along with the sliders 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"
12 ]
13 },
14 {
15 "cell_type": "code",
16 "execution_count": null,
17 "metadata": {},
18 "outputs": [],
19 "source": [
20 "import numpy as np\n",
21 "from scipy.stats import norm\n",
22 "\n",
23 "from ipywidgets import FloatSlider, HBox, VBox\n",
24 "import bqplot.pyplot as plt"
25 ]
26 },
27 {
28 "cell_type": "code",
29 "execution_count": null,
30 "metadata": {},
31 "outputs": [],
32 "source": [
33 "x = np.linspace(-10, 10, 200)\n",
34 "y = norm.pdf(x)\n",
35 "\n",
36 "# plot the gaussian density\n",
37 "title_tmpl = 'Gaussian Density (mu = {} and sigma = {})'\n",
38 "pdf_fig = plt.figure(title=title_tmpl.format(0, 1))\n",
39 "pdf_line = plt.plot(x, y, 'm', stroke_width=3)"
40 ]
41 },
42 {
43 "cell_type": "code",
44 "execution_count": null,
45 "metadata": {},
46 "outputs": [],
47 "source": [
48 "# use two sliders to represent mu and sigma\n",
49 "mu_slider = FloatSlider(description='mu', value=0, min=-5, max=5, step=.1)\n",
50 "sigma_slider = FloatSlider(description='sigma', value=1, min=0.1, max=5, step=.1)\n",
51 "\n",
52 "slider_layout = HBox([mu_slider, sigma_slider])"
53 ]
54 },
55 {
56 "cell_type": "code",
57 "execution_count": null,
58 "metadata": {},
59 "outputs": [],
60 "source": [
61 "def update_density(change):\n",
62 " new_mu = mu_slider.value\n",
63 " new_sigma = sigma_slider.value\n",
64 " # update the y attribute of the plot with the new pdf\n",
65 " # computed using new mu and sigma values\n",
66 " pdf_line.y = norm.pdf(x, new_mu, new_sigma)\n",
67 " \n",
68 " # also update the fig title\n",
69 " pdf_fig.title = title_tmpl.format(new_mu, new_sigma)\n",
70 "\n",
71 "# register the above callback with the 'value' trait of the sliders\n",
72 "for slider in [mu_slider, sigma_slider]:\n",
73 " slider.observe(update_density, 'value')"
74 ]
75 },
76 {
77 "cell_type": "code",
78 "execution_count": null,
79 "metadata": {
80 "scrolled": false
81 },
82 "outputs": [],
83 "source": [
84 "# now put all the widgets together into a simple dashboard\n",
85 "# the plot should update now when the slider values are updated!\n",
86 "final_layout = VBox([pdf_fig, slider_layout])\n",
87 "final_layout"
88 ]
89 }
90 ],
91 "metadata": {
92 "kernelspec": {
93 "display_name": "Python 3",
94 "language": "python",
95 "name": "python3"
96 },
97 "language_info": {
98 "codemirror_mode": {
99 "name": "ipython",
100 "version": 3
101 },
102 "file_extension": ".py",
103 "mimetype": "text/x-python",
104 "name": "python",
105 "nbconvert_exporter": "python",
106 "pygments_lexer": "ipython3",
107 "version": "3.7.6"
108 }
109 },
110 "nbformat": 4,
111 "nbformat_minor": 2
112 }