| 1 |
{ |
| 2 |
"cells": [ |
| 3 |
{ |
| 4 |
"cell_type": "markdown", |
| 5 |
"metadata": {}, |
| 6 |
"source": [ |
| 7 |
"## Wealth Of Nations Dashboard\n", |
| 8 |
"1. Use the dropdown to select the data type\n", |
| 9 |
"2. Use the slider to show the heatmap for a specific year\n", |
| 10 |
"3. Use the animation button to animate the choropleth for all the years\n", |
| 11 |
"4. Clicking on any country toggles the display of timeseries for that country. (Clicking on oceans un-selects all the countries)" |
| 12 |
] |
| 13 |
}, |
| 14 |
{ |
| 15 |
"cell_type": "code", |
| 16 |
"execution_count": null, |
| 17 |
"metadata": {}, |
| 18 |
"outputs": [], |
| 19 |
"source": [ |
| 20 |
"import numpy as np\n", |
| 21 |
"import pandas as pd\n", |
| 22 |
"\n", |
| 23 |
"import ipywidgets as widgets\n", |
| 24 |
"import bqplot as bq\n", |
| 25 |
"import bqplot.pyplot as plt" |
| 26 |
] |
| 27 |
}, |
| 28 |
{ |
| 29 |
"cell_type": "code", |
| 30 |
"execution_count": null, |
| 31 |
"metadata": {}, |
| 32 |
"outputs": [], |
| 33 |
"source": [ |
| 34 |
"initial_year = 1800" |
| 35 |
] |
| 36 |
}, |
| 37 |
{ |
| 38 |
"cell_type": "code", |
| 39 |
"execution_count": null, |
| 40 |
"metadata": { |
| 41 |
"scrolled": false |
| 42 |
}, |
| 43 |
"outputs": [], |
| 44 |
"source": [ |
| 45 |
"data = pd.read_json('nations.json')\n", |
| 46 |
"country_codes = pd.read_csv('world_map_codes.csv')\n", |
| 47 |
"country_code_map = dict(zip(country_codes['ISON3'], country_codes['Name']))" |
| 48 |
] |
| 49 |
}, |
| 50 |
{ |
| 51 |
"cell_type": "code", |
| 52 |
"execution_count": null, |
| 53 |
"metadata": {}, |
| 54 |
"outputs": [], |
| 55 |
"source": [ |
| 56 |
"country_data = data.merge(country_codes, \n", |
| 57 |
" left_on='name', \n", |
| 58 |
" right_on='Name')[['name', 'ISON3', 'region', 'income', 'lifeExpectancy', 'population']]\\\n", |
| 59 |
" .rename(columns={'ISON3': 'id'})\\\n", |
| 60 |
" .set_index('id')" |
| 61 |
] |
| 62 |
}, |
| 63 |
{ |
| 64 |
"cell_type": "code", |
| 65 |
"execution_count": null, |
| 66 |
"metadata": {}, |
| 67 |
"outputs": [], |
| 68 |
"source": [ |
| 69 |
"def get_time_series(col):\n", |
| 70 |
" time_series_df = country_data[col]\\\n", |
| 71 |
" .apply(dict)\\\n", |
| 72 |
" .apply(pd.Series)\n", |
| 73 |
" time_series_df.index.name = None\n", |
| 74 |
" time_series_df.columns = time_series_df.columns.astype('int')\n", |
| 75 |
" \n", |
| 76 |
" return time_series_df" |
| 77 |
] |
| 78 |
}, |
| 79 |
{ |
| 80 |
"cell_type": "code", |
| 81 |
"execution_count": null, |
| 82 |
"metadata": {}, |
| 83 |
"outputs": [], |
| 84 |
"source": [ |
| 85 |
"income_time_series = get_time_series('income')\n", |
| 86 |
"life_exp_time_series = get_time_series('lifeExpectancy')\n", |
| 87 |
"pop_time_series = get_time_series('population')" |
| 88 |
] |
| 89 |
}, |
| 90 |
{ |
| 91 |
"cell_type": "code", |
| 92 |
"execution_count": null, |
| 93 |
"metadata": { |
| 94 |
"scrolled": true |
| 95 |
}, |
| 96 |
"outputs": [], |
| 97 |
"source": [ |
| 98 |
"income_min, income_max = income_time_series.min().min(), income_time_series.max().max()\n", |
| 99 |
"life_exp_min, life_exp_max = life_exp_time_series.min().min(), life_exp_time_series.max().max()\n", |
| 100 |
"pop_min, pop_max = pop_time_series.min().min(), pop_time_series.max().max()" |
| 101 |
] |
| 102 |
}, |
| 103 |
{ |
| 104 |
"cell_type": "code", |
| 105 |
"execution_count": null, |
| 106 |
"metadata": { |
| 107 |
"scrolled": true |
| 108 |
}, |
| 109 |
"outputs": [], |
| 110 |
"source": [ |
| 111 |
"data_dict = {'Income': income_time_series,\n", |
| 112 |
" 'Population': pop_time_series,\n", |
| 113 |
" 'Life Expectancy': life_exp_time_series}\n", |
| 114 |
"\n", |
| 115 |
"min_max_dict = {'Income': (np.log(income_min), np.log(income_max)), \n", |
| 116 |
" 'Population': (np.log(pop_min), np.log(pop_max)),\n", |
| 117 |
" 'Life Expectancy': (life_exp_min, life_exp_max)}\n", |
| 118 |
"\n", |
| 119 |
"color_scheme_dict = {'Income': 'Greens',\n", |
| 120 |
" 'Population': 'Blues',\n", |
| 121 |
" 'Life Expectancy': 'Oranges'}\n", |
| 122 |
"\n", |
| 123 |
"data_type_dropdown = widgets.Dropdown(description='Data Type', \n", |
| 124 |
" options=['Income', 'Life Expectancy', 'Population'],\n", |
| 125 |
" value='Population')\n", |
| 126 |
"\n", |
| 127 |
"year_slider = widgets.IntSlider(description='year', min=1800, max=2008, \n", |
| 128 |
" continuous_update=False,\n", |
| 129 |
" layout=widgets.Layout(width='300px'))\n", |
| 130 |
"\n", |
| 131 |
"play_button = widgets.Play(min=1800, max=2008, interval=10)\n", |
| 132 |
"widgets.jslink((play_button, 'value'), (year_slider, 'value'))\n", |
| 133 |
"\n", |
| 134 |
"# world map\n", |
| 135 |
"world_map_fig_title_tmpl = 'World {data_type} for year {year}'\n", |
| 136 |
"world_map_fig = plt.figure(layout=widgets.Layout(width='1000px', height='500px'),\n", |
| 137 |
" fig_margin=dict(top=45, bottom=10, left=20, right=20))\n", |
| 138 |
"tooltip = bq.Tooltip(fields=['name'], labels=['Country'])\n", |
| 139 |
"\n", |
| 140 |
"plt.scales(scales={'projection': bq.Mercator(), 'color': bq.ColorScale()})\n", |
| 141 |
"\n", |
| 142 |
"world_map = plt.geo(map_data='WorldMap', tooltip=tooltip, \n", |
| 143 |
" interactions={'click': 'select', 'hover': 'tooltip'},\n", |
| 144 |
" axes_options={'color': {'visible': False}},\n", |
| 145 |
" selected_styles={'selected_fill': 'yellow'},\n", |
| 146 |
" colors={'default_color': 'Grey'})\n", |
| 147 |
"\n", |
| 148 |
"# data time series fig\n", |
| 149 |
"ts_fig_title_tmpl = '{data_type} Time Series'\n", |
| 150 |
"time_series_fig = plt.figure(layout=widgets.Layout(width='1000px', height='400px'),\n", |
| 151 |
" fig_margin=dict(top=40, bottom=30, left=60, right=40))\n", |
| 152 |
"country_time_series = plt.plot(list(pop_time_series.columns), [], \n", |
| 153 |
" axes_options={'x': {'label': 'Year'},\n", |
| 154 |
" 'y': {'tick_format': '~s'}},\n", |
| 155 |
" display_legend=True)\n", |
| 156 |
"\n", |
| 157 |
"# link widgets\n", |
| 158 |
"def on_data_type_update(*args):\n", |
| 159 |
" global data_time_series\n", |
| 160 |
" # reset color and selected attributes of world_map\n", |
| 161 |
" world_map.color = {}\n", |
| 162 |
" world_map.selected = []\n", |
| 163 |
" \n", |
| 164 |
" data_type = data_type_dropdown.value\n", |
| 165 |
" data_time_series = data_dict[data_type]\n", |
| 166 |
" \n", |
| 167 |
" scale_min, scale_max = min_max_dict[data_type]\n", |
| 168 |
" color_scale = world_map.scales['color']\n", |
| 169 |
" color_scale.scheme = color_scheme_dict[data_type]\n", |
| 170 |
" color_scale.min = scale_min\n", |
| 171 |
" color_scale.max = scale_max\n", |
| 172 |
" year_slider.value = initial_year\n", |
| 173 |
"\n", |
| 174 |
"data_type_dropdown.observe(on_data_type_update, 'value')\n", |
| 175 |
" \n", |
| 176 |
"def update_color(*args):\n", |
| 177 |
" data_type = data_type_dropdown.value\n", |
| 178 |
" year = year_slider.value\n", |
| 179 |
" \n", |
| 180 |
" if year in data_time_series.columns:\n", |
| 181 |
" if data_type in ['Population', 'Income']:\n", |
| 182 |
" color_data = np.log(data_time_series[year])\n", |
| 183 |
" else:\n", |
| 184 |
" color_data = data_time_series[year]\n", |
| 185 |
" world_map.color = dict(color_data)\n", |
| 186 |
" else:\n", |
| 187 |
" world_map.color = {}\n", |
| 188 |
" \n", |
| 189 |
" world_map_fig.title = world_map_fig_title_tmpl.format(data_type=data_type, year=year)\n", |
| 190 |
"\n", |
| 191 |
"year_slider.observe(update_color, 'value')\n", |
| 192 |
"\n", |
| 193 |
"def plot_time_series_for_country(*args):\n", |
| 194 |
" if world_map.selected is not None and len(world_map.selected) > 0:\n", |
| 195 |
" selected_countries = world_map.selected\n", |
| 196 |
" country_time_series.y = data_time_series.loc[selected_countries]\n", |
| 197 |
" country_time_series.labels = [country_code_map[c_id] for c_id in selected_countries]\n", |
| 198 |
" else:\n", |
| 199 |
" country_time_series.labels = []\n", |
| 200 |
" country_time_series.y = []\n", |
| 201 |
" time_series_fig.title = ts_fig_title_tmpl.format(data_type=data_type_dropdown.value)\n", |
| 202 |
"world_map.observe(plot_time_series_for_country, 'selected')\n", |
| 203 |
" \n", |
| 204 |
"on_data_type_update(None)\n", |
| 205 |
"\n", |
| 206 |
"widgets.VBox([world_map_fig, \n", |
| 207 |
" widgets.HBox([time_series_fig, \n", |
| 208 |
" widgets.VBox([data_type_dropdown, year_slider, play_button])])\n", |
| 209 |
" ])" |
| 210 |
] |
| 211 |
} |
| 212 |
], |
| 213 |
"metadata": { |
| 214 |
"kernelspec": { |
| 215 |
"display_name": "Python 3", |
| 216 |
"language": "python", |
| 217 |
"name": "python3" |
| 218 |
}, |
| 219 |
"language_info": { |
| 220 |
"codemirror_mode": { |
| 221 |
"name": "ipython", |
| 222 |
"version": 3 |
| 223 |
}, |
| 224 |
"file_extension": ".py", |
| 225 |
"mimetype": "text/x-python", |
| 226 |
"name": "python", |
| 227 |
"nbconvert_exporter": "python", |
| 228 |
"pygments_lexer": "ipython3", |
| 229 |
"version": "3.8.2" |
| 230 |
} |
| 231 |
}, |
| 232 |
"nbformat": 4, |
| 233 |
"nbformat_minor": 1 |
| 234 |
} |