|
15 | 15 | },
|
16 | 16 | "outputs": [],
|
17 | 17 | "source": [
|
18 |
| - "# Required imports\n", |
19 | 18 | "import pandas as pd\n",
|
20 |
| - "from bqplot import (LogScale, LinearScale, OrdinalColorScale, ColorAxis,\n", |
21 |
| - " Axis, Scatter, Lines, CATEGORY10, Label, Figure)\n", |
22 |
| - "from bqplot.default_tooltip import Tooltip\n", |
23 |
| - "from ipywidgets import VBox, IntSlider, Button\n", |
24 |
| - "from IPython.display import display\n", |
25 |
| - "import os\n", |
26 | 19 | "import numpy as np\n",
|
27 |
| - "from time import sleep" |
| 20 | + "import os\n", |
| 21 | + "\n", |
| 22 | + "from bqplot import (\n", |
| 23 | + " LogScale, LinearScale, OrdinalColorScale, ColorAxis,\n", |
| 24 | + " Axis, Scatter, Lines, CATEGORY10, Label, Figure, Tooltip\n", |
| 25 | + ")\n", |
| 26 | + "\n", |
| 27 | + "from ipywidgets import VBox, IntSlider, Play, jsdlink" |
28 | 28 | ]
|
29 | 29 | },
|
30 | 30 | {
|
|
35 | 35 | },
|
36 | 36 | "outputs": [],
|
37 | 37 | "source": [
|
38 |
| - "# The GUI starts with this year\n", |
39 | 38 | "initial_year = 1800"
|
40 | 39 | ]
|
41 | 40 | },
|
|
197 | 196 | },
|
198 | 197 | "outputs": [],
|
199 | 198 | "source": [
|
200 |
| - "ax_y = Axis(label='Life Expectancy', scale=y_sc, orientation='vertical', side='left')\n", |
201 |
| - "ax_x = Axis(label='Income per Capita', scale=x_sc)" |
| 199 | + "ax_y = Axis(label='Life Expectancy', scale=y_sc, orientation='vertical', side='left', grid_lines='solid')\n", |
| 200 | + "ax_x = Axis(label='Income per Capita', scale=x_sc, grid_lines='solid')" |
202 | 201 | ]
|
203 | 202 | },
|
204 | 203 | {
|
|
256 | 255 | "#### Creating the Figure"
|
257 | 256 | ]
|
258 | 257 | },
|
| 258 | + { |
| 259 | + "cell_type": "code", |
| 260 | + "execution_count": null, |
| 261 | + "metadata": { |
| 262 | + "collapsed": true |
| 263 | + }, |
| 264 | + "outputs": [], |
| 265 | + "source": [ |
| 266 | + "time_interval = 50" |
| 267 | + ] |
| 268 | + }, |
259 | 269 | {
|
260 | 270 | "cell_type": "code",
|
261 | 271 | "execution_count": null,
|
|
265 | 275 | "outputs": [],
|
266 | 276 | "source": [
|
267 | 277 | "fig = Figure(marks=[wealth_scat, year_label, nation_line], axes=[ax_x, ax_y],\n",
|
268 |
| - " title='Health and Wealth of Nations', background_style={'fill': 'White'},\n", |
269 |
| - " animation_duration=100)" |
| 278 | + " title='Health and Wealth of Nations', animation_duration=time_interval)" |
270 | 279 | ]
|
271 | 280 | },
|
272 | 281 | {
|
|
289 | 298 | "year_slider = IntSlider(min=1800, max=2008, step=1, description='Year', value=initial_year)"
|
290 | 299 | ]
|
291 | 300 | },
|
292 |
| - { |
293 |
| - "cell_type": "code", |
294 |
| - "execution_count": null, |
295 |
| - "metadata": { |
296 |
| - "collapsed": true |
297 |
| - }, |
298 |
| - "outputs": [], |
299 |
| - "source": [ |
300 |
| - "animate_button = Button(description='Play', background_color='MediumSeaGreen', color='Black', icon='fa-play')" |
301 |
| - ] |
302 |
| - }, |
303 | 301 | {
|
304 | 302 | "cell_type": "markdown",
|
305 | 303 | "metadata": {},
|
|
341 | 339 | },
|
342 | 340 | "outputs": [],
|
343 | 341 | "source": [
|
344 |
| - "def year_changed(values):\n", |
| 342 | + "def year_changed(new):\n", |
345 | 343 | " wealth_scat.x, wealth_scat.y, wealth_scat.size = get_data(year_slider.value)\n",
|
346 | 344 | " year_label.text = str(year_slider.value)\n",
|
347 |
| - " \n", |
| 345 | + "\n", |
348 | 346 | "year_slider.observe(year_changed, 'value')"
|
349 | 347 | ]
|
350 | 348 | },
|
351 | 349 | {
|
352 | 350 | "cell_type": "markdown",
|
353 | 351 | "metadata": {},
|
354 | 352 | "source": [
|
355 |
| - "#### Defining the callback for the button\n", |
356 |
| - "\n", |
357 |
| - "When the `Button` is clicked, we trigger the animation by changing the slider value through all the years for which data is available. The change in the `Slider` value triggers a change in the plot." |
| 353 | + "#### Add an animation button" |
358 | 354 | ]
|
359 | 355 | },
|
360 | 356 | {
|
361 | 357 | "cell_type": "code",
|
362 | 358 | "execution_count": null,
|
363 | 359 | "metadata": {
|
364 |
| - "collapsed": true |
| 360 | + "collapsed": false |
365 | 361 | },
|
366 | 362 | "outputs": [],
|
367 | 363 | "source": [
|
368 |
| - "def button_clicked(value):\n", |
369 |
| - " animate_button.visible = False\n", |
370 |
| - " for i in range(1800, 2009, 1):\n", |
371 |
| - " year_slider.value = i\n", |
372 |
| - " sleep(0.05)\n", |
373 |
| - " animate_button.visible = True\n", |
374 |
| - "\n", |
375 |
| - "animate_button.on_click(button_clicked)" |
| 364 | + "play_button = Play(min=1800, max=2008, interval=time_interval)\n", |
| 365 | + "jsdlink((play_button, 'value'), (year_slider, 'value'))" |
376 | 366 | ]
|
377 | 367 | },
|
378 | 368 | {
|
|
391 | 381 | },
|
392 | 382 | "outputs": [],
|
393 | 383 | "source": [
|
394 |
| - "display(VBox([animate_button, fig, year_slider]))" |
| 384 | + "VBox([play_button, fig, year_slider])" |
395 | 385 | ]
|
396 | 386 | }
|
397 | 387 | ],
|
|
0 commit comments