|
20 | 20 | "# Enabling the `widget` backend.\n",
|
21 | 21 | "# This requires jupyter-matplotlib a.k.a. ipympl.\n",
|
22 | 22 | "# 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" |
24 | 27 | ]
|
25 | 28 | },
|
26 | 29 | {
|
|
30 | 33 | "outputs": [],
|
31 | 34 | "source": [
|
32 | 35 | "# Testing matplotlib interactions with a simple plot\n",
|
33 |
| - "import matplotlib.pyplot as plt\n", |
34 |
| - "import numpy as np\n", |
35 |
| - "\n", |
36 | 36 | "fig = plt.figure()\n",
|
37 | 37 | "plt.plot(np.sin(np.linspace(0, 20, 100)));"
|
38 | 38 | ]
|
|
65 | 65 | "fig.canvas"
|
66 | 66 | ]
|
67 | 67 | },
|
| 68 | + { |
| 69 | + "cell_type": "markdown", |
| 70 | + "metadata": {}, |
| 71 | + "source": [ |
| 72 | + "# 3D plotting" |
| 73 | + ] |
| 74 | + }, |
68 | 75 | {
|
69 | 76 | "cell_type": "code",
|
70 | 77 | "execution_count": null,
|
|
85 | 92 | "plt.show()"
|
86 | 93 | ]
|
87 | 94 | },
|
| 95 | + { |
| 96 | + "cell_type": "markdown", |
| 97 | + "metadata": {}, |
| 98 | + "source": [ |
| 99 | + "# Subplots" |
| 100 | + ] |
| 101 | + }, |
88 | 102 | {
|
89 | 103 | "cell_type": "code",
|
90 | 104 | "execution_count": null,
|
|
138 | 152 | "fig.canvas.toolbar_visible = False"
|
139 | 153 | ]
|
140 | 154 | },
|
| 155 | + { |
| 156 | + "cell_type": "markdown", |
| 157 | + "metadata": {}, |
| 158 | + "source": [ |
| 159 | + "# Interactions with other widgets and layouting" |
| 160 | + ] |
| 161 | + }, |
141 | 162 | {
|
142 | 163 | "cell_type": "code",
|
143 | 164 | "execution_count": null,
|
|
146 | 167 | "source": [
|
147 | 168 | "# When using the `widget` backend from ipympl,\n",
|
148 | 169 | "# 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", |
150 | 171 | "\n",
|
151 | 172 | "# 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", |
154 | 174 | "\n",
|
155 | 175 | "plt.ioff()\n",
|
156 | 176 | "\n",
|
157 | 177 | "slider = FloatSlider(\n",
|
158 |
| - " orientation='vertical',\n", |
| 178 | + " orientation='horizontal',\n", |
| 179 | + " description='Factor:',\n", |
159 | 180 | " value=1.0,\n",
|
160 | 181 | " min=0.02,\n",
|
161 | 182 | " max=2.0\n",
|
162 | 183 | ")\n",
|
163 | 184 | "\n",
|
| 185 | + "slider.layout.margin = '0px 30% 0px 30%'\n", |
| 186 | + "slider.layout.width = '40%'\n", |
| 187 | + "\n", |
164 | 188 | "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", |
165 | 192 | "\n",
|
166 | 193 | "x = np.linspace(0, 20, 500)\n",
|
167 | 194 | "\n",
|
168 |
| - "lines = plt.plot(x, np.sin(slider.value * x))\n", |
| 195 | + "lines = plt.plot(x, np.sin(slider.value * x))\n", |
169 | 196 | "\n",
|
170 | 197 | "def update_lines(change):\n",
|
| 198 | + " plt.title('Plotting: y=sin({} * x)'.format(change.new))\n", |
171 | 199 | " lines[0].set_data(x, np.sin(change.new * x))\n",
|
172 | 200 | " fig.canvas.draw()\n",
|
173 | 201 | " fig.canvas.flush_events()\n",
|
174 | 202 | "\n",
|
175 | 203 | "slider.observe(update_lines, names='value')\n",
|
176 |
| - "fig.canvas.toolbar_visible = False\n", |
177 | 204 | "\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 | + ")" |
179 | 210 | ]
|
180 | 211 | }
|
181 | 212 | ],
|
|
0 commit comments