{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": { "tags": [ "parameters" ] }, "outputs": [], "source": [ "flex_source_code = \"https://github.com/danielfrg/jupyter-flex/blob/master/examples/widgets/mpl-histogram.ipynb\"" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "import ipywidgets as widgets\n", "from IPython.display import clear_output, display\n", "\n", "import matplotlib.pyplot as plt\n", "from IPython.display import set_matplotlib_formats\n", "%matplotlib inline\n", "set_matplotlib_formats('svg')\n", "\n", "np.random.seed(42)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# fig_size = plt.rcParams[\"figure.figsize\"]\n", "# fig_size[0] = 6 * 3\n", "# fig_size[1] = 4 * 3\n", "# plt.rcParams[\"figure.figsize\"] = fig_size" ] }, { "cell_type": "markdown", "metadata": { "tags": [ "size=250" ] }, "source": [ "## Sidebar" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "tags": [ "inputs" ] }, "outputs": [], "source": [ "mu_label = widgets.Label(value=\"Mean:\")\n", "mu_var = widgets.BoundedIntText(value=100, min=10, max=300)\n", "sigma_label = widgets.Label(value=\"Sigma:\")\n", "sigma_var = widgets.BoundedIntText(value=15, min=10, max=50)\n", "bins_label = widgets.Label(value=\"Bins:\")\n", "bins_var = widgets.IntSlider(value=50, min=1, max=100, step=1)\n", "\n", "widgets.VBox([mu_label, mu_var, sigma_label, sigma_var, bins_label, bins_var])" ] }, { "cell_type": "markdown", "metadata": { "tags": [ "size=750" ] }, "source": [ "## Column" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "out = widgets.Output()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def on_value_change(change):\n", " mu = mu_var.value\n", " sigma = sigma_var.value\n", " num_bins = bins_var.value\n", "\n", " with out:\n", " fig, ax = plt.subplots()\n", "\n", " # the histogram of the data\n", " x = mu + sigma * np.random.randn(437)\n", " n, bins, patches = ax.hist(x, num_bins, density=1)\n", "\n", " # add a 'best fit' line\n", " y = ((1 / (np.sqrt(2 * np.pi) * sigma)) * np.exp(-0.5 * (1 / sigma * (bins - mu))**2))\n", " ax.plot(bins, y, '--')\n", " \n", " ax.set_xlabel('X')\n", " ax.set_ylabel('Probability density')\n", " ax.set_title(f'Histogram with: mu={mu}, sigma={sigma}, bins={num_bins}')\n", "\n", " clear_output(wait=True)\n", " plt.show(fig)\n", "\n", "mu_var.observe(on_value_change, names=\"value\")\n", "sigma_var.observe(on_value_change, names=\"value\")\n", "bins_var.observe(on_value_change, names=\"value\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "tags": [ "chart" ] }, "outputs": [], "source": [ "on_value_change(None)\n", "out" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "celltoolbar": "Tags", "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 }