Skip to content

Commit 94902ce

Browse files
committed
Improve example Notebook
Using the ipywidgets new layouts Signed-off-by: martinRenou <[email protected]>
1 parent 6577876 commit 94902ce

File tree

1 file changed

+42
-11
lines changed

1 file changed

+42
-11
lines changed

examples/ipympl.ipynb

Lines changed: 42 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,10 @@
2020
"# Enabling the `widget` backend.\n",
2121
"# This requires jupyter-matplotlib a.k.a. ipympl.\n",
2222
"# ipympl can be install via pip or conda.\n",
23-
"%matplotlib widget"
23+
"%matplotlib widget\n",
24+
"\n",
25+
"import matplotlib.pyplot as plt\n",
26+
"import numpy as np"
2427
]
2528
},
2629
{
@@ -30,9 +33,6 @@
3033
"outputs": [],
3134
"source": [
3235
"# Testing matplotlib interactions with a simple plot\n",
33-
"import matplotlib.pyplot as plt\n",
34-
"import numpy as np\n",
35-
"\n",
3636
"fig = plt.figure()\n",
3737
"plt.plot(np.sin(np.linspace(0, 20, 100)));"
3838
]
@@ -65,6 +65,13 @@
6565
"fig.canvas"
6666
]
6767
},
68+
{
69+
"cell_type": "markdown",
70+
"metadata": {},
71+
"source": [
72+
"# 3D plotting"
73+
]
74+
},
6875
{
6976
"cell_type": "code",
7077
"execution_count": null,
@@ -85,6 +92,13 @@
8592
"plt.show()"
8693
]
8794
},
95+
{
96+
"cell_type": "markdown",
97+
"metadata": {},
98+
"source": [
99+
"# Subplots"
100+
]
101+
},
88102
{
89103
"cell_type": "code",
90104
"execution_count": null,
@@ -138,6 +152,13 @@
138152
"fig.canvas.toolbar_visible = False"
139153
]
140154
},
155+
{
156+
"cell_type": "markdown",
157+
"metadata": {},
158+
"source": [
159+
"# Interactions with other widgets and layouting"
160+
]
161+
},
141162
{
142163
"cell_type": "code",
143164
"execution_count": null,
@@ -146,36 +167,46 @@
146167
"source": [
147168
"# When using the `widget` backend from ipympl,\n",
148169
"# fig.canvas is a proper Jupyter interactive widget, which can be embedded in\n",
149-
"# Layout classes like HBox and Vbox.\n",
170+
"# an ipywidgets layout. See https://ipywidgets.readthedocs.io/en/stable/examples/Layout%20Templates.html\n",
150171
"\n",
151172
"# One can bound figure attributes to other widget values.\n",
152-
"\n",
153-
"from ipywidgets import HBox, FloatSlider\n",
173+
"from ipywidgets import AppLayout, FloatSlider\n",
154174
"\n",
155175
"plt.ioff()\n",
156176
"\n",
157177
"slider = FloatSlider(\n",
158-
" orientation='vertical',\n",
178+
" orientation='horizontal',\n",
179+
" description='Factor:',\n",
159180
" value=1.0,\n",
160181
" min=0.02,\n",
161182
" max=2.0\n",
162183
")\n",
163184
"\n",
185+
"slider.layout.margin = '0px 30% 0px 30%'\n",
186+
"slider.layout.width = '40%'\n",
187+
"\n",
164188
"fig = plt.figure()\n",
189+
"fig.canvas.header_visible = False\n",
190+
"fig.canvas.layout.min_height = '400px'\n",
191+
"plt.title('Plotting: y=sin({} * x)'.format(slider.value))\n",
165192
"\n",
166193
"x = np.linspace(0, 20, 500)\n",
167194
"\n",
168-
"lines = plt.plot(x, np.sin(slider.value * x))\n",
195+
"lines = plt.plot(x, np.sin(slider.value * x))\n",
169196
"\n",
170197
"def update_lines(change):\n",
198+
" plt.title('Plotting: y=sin({} * x)'.format(change.new))\n",
171199
" lines[0].set_data(x, np.sin(change.new * x))\n",
172200
" fig.canvas.draw()\n",
173201
" fig.canvas.flush_events()\n",
174202
"\n",
175203
"slider.observe(update_lines, names='value')\n",
176-
"fig.canvas.toolbar_visible = False\n",
177204
"\n",
178-
"HBox([slider, fig.canvas])"
205+
"AppLayout(\n",
206+
" center=fig.canvas,\n",
207+
" footer=slider,\n",
208+
" pane_heights=[0, 6, 1]\n",
209+
")"
179210
]
180211
}
181212
],

0 commit comments

Comments
 (0)