Skip to content

Instantly share code, notes, and snippets.

@karlafej
Created August 10, 2018 11:26
Show Gist options
  • Save karlafej/f7d47d80aad037586b6b9356918998f2 to your computer and use it in GitHub Desktop.
Save karlafej/f7d47d80aad037586b6b9356918998f2 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"%matplotlib inline"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"from ipywidgets import Layout\n",
"from bqplot import LinearScale, Hist, Axis, Figure, Label\n",
"from ipywidgets import HBox, VBox, IntSlider, Play, jslink"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Generate data"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"data = [np.random.normal(mu+1, 0.1, 20) for mu in range(20)]"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"data = np.ravel(data)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Make plot"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"x_sc = LinearScale(min=0,max=21)\n",
"y_sc = LinearScale()\n",
"\n",
"hist = Hist(sample=data, scales={'sample': x_sc, 'count': y_sc})\n",
"ax_x = Axis(scale=x_sc, tick_format='0.2f')\n",
"ax_y = Axis(scale=y_sc, orientation='vertical')"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"initial_bins = 30\n",
"hist.bins = initial_bins\n",
"\n",
"bin_label = Label(x=[0.81], y=[0.08], default_size=20, font_weight='bolder', colors=['black'], stroke='white',\n",
" text=['Bins: ' + str(initial_bins)], enable_move=True)"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "5499964b495b4d16bdf787f688cc7b81",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"Figure(axes=[Axis(scale=LinearScale(max=21.0, min=0.0), tick_format='0.2f'), Axis(orientation='vertical', scal…"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"Figure(marks=[hist, bin_label], axes=[ax_x, ax_y], padding_y=0)"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [],
"source": [
"hist.colors = ['seagreen'] # change color\n",
"hist.stroke = 'seagreen'\n",
"hist.opacities = [0.8] * hist.bins"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Animation"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [],
"source": [
"time_interval = 150"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [],
"source": [
"fig = Figure(marks=[hist, bin_label], axes=[ax_x, ax_y], padding_y=0,\n",
" animation_duration=time_interval)"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [],
"source": [
"bin_slider = IntSlider(min=10, max=55, step=1, description='Bins', value=10)"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [],
"source": [
"def bins_changed(change):\n",
" hist.bins = bin_slider.value\n",
" hist.opacities = [0.8] * hist.bins\n",
" bin_label.text = ['Bins: '+str(bin_slider.value)]\n",
"\n",
"bin_slider.observe(bins_changed, 'value')"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [],
"source": [
"play_button = Play(min=10, max=55, interval=time_interval)\n",
"jslink((play_button, 'value'), (bin_slider, 'value'))"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "711412f3d4b6449c8d09d92823b42702",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"VBox(children=(HBox(children=(Play(value=10, interval=150, max=55, min=10), IntSlider(value=10, description='B…"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"VBox([HBox([play_button, bin_slider]), fig])"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"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.6.6"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment