| 1 |
{ |
| 2 |
"cells": [ |
| 3 |
{ |
| 4 |
"cell_type": "markdown", |
| 5 |
"metadata": {}, |
| 6 |
"source": [ |
| 7 |
"This is a `bqplot` recreation of Mike Bostock's [Wealth of Nations](https://bost.ocks.org/mike/nations/). This was also done by [Gapminder](http://www.gapminder.org/world/#$majorMode=chart$is;shi=t;ly=2003;lb=f;il=t;fs=11;al=30;stl=t;st=t;nsl=t;se=t$wst;tts=C$ts;sp=5.59290322580644;ti=2013$zpv;v=0$inc_x;mmid=XCOORDS;iid=phAwcNAVuyj1jiMAkmq1iMg;by=ind$inc_y;mmid=YCOORDS;iid=phAwcNAVuyj2tPLxKvvnNPA;by=ind$inc_s;uniValue=8.21;iid=phAwcNAVuyj0XOoBL_n5tAQ;by=ind$inc_c;uniValue=255;gid=CATID0;by=grp$map_x;scale=log;dataMin=194;dataMax=96846$map_y;scale=lin;dataMin=23;dataMax=86$map_s;sma=49;smi=2.65$cd;bd=0$inds=;modified=60). It is originally based on a TED Talk by [Hans Rosling](http://www.ted.com/talks/hans_rosling_shows_the_best_stats_you_ve_ever_seen)." |
| 8 |
] |
| 9 |
}, |
| 10 |
{ |
| 11 |
"cell_type": "code", |
| 12 |
"execution_count": null, |
| 13 |
"metadata": {}, |
| 14 |
"outputs": [], |
| 15 |
"source": [ |
| 16 |
"import numpy as np\n", |
| 17 |
"import pandas as pd\n", |
| 18 |
"\n", |
| 19 |
"import ipywidgets as widgets\n", |
| 20 |
"import bqplot as bq\n", |
| 21 |
"import bqplot.pyplot as plt" |
| 22 |
] |
| 23 |
}, |
| 24 |
{ |
| 25 |
"cell_type": "code", |
| 26 |
"execution_count": null, |
| 27 |
"metadata": {}, |
| 28 |
"outputs": [], |
| 29 |
"source": [ |
| 30 |
"initial_year = 1800" |
| 31 |
] |
| 32 |
}, |
| 33 |
{ |
| 34 |
"cell_type": "code", |
| 35 |
"execution_count": null, |
| 36 |
"metadata": { |
| 37 |
"scrolled": false |
| 38 |
}, |
| 39 |
"outputs": [], |
| 40 |
"source": [ |
| 41 |
"data = pd.read_json('nations.json')" |
| 42 |
] |
| 43 |
}, |
| 44 |
{ |
| 45 |
"cell_type": "code", |
| 46 |
"execution_count": null, |
| 47 |
"metadata": {}, |
| 48 |
"outputs": [], |
| 49 |
"source": [ |
| 50 |
"def clean_data(data):\n", |
| 51 |
" for column in ['income', 'lifeExpectancy', 'population']:\n", |
| 52 |
" data = data.drop(data[data[column].apply(len) <= 4].index)\n", |
| 53 |
" return data\n", |
| 54 |
"\n", |
| 55 |
"def extrap_interp(data):\n", |
| 56 |
" data = np.array(data)\n", |
| 57 |
" x_range = np.arange(1800, 2009, 1.)\n", |
| 58 |
" y_range = np.interp(x_range, data[:, 0], data[:, 1])\n", |
| 59 |
" return y_range\n", |
| 60 |
"\n", |
| 61 |
"def extrap_data(data):\n", |
| 62 |
" for column in ['income', 'lifeExpectancy', 'population']:\n", |
| 63 |
" data[column] = data[column].apply(extrap_interp)\n", |
| 64 |
" return data" |
| 65 |
] |
| 66 |
}, |
| 67 |
{ |
| 68 |
"cell_type": "code", |
| 69 |
"execution_count": null, |
| 70 |
"metadata": {}, |
| 71 |
"outputs": [], |
| 72 |
"source": [ |
| 73 |
"data = clean_data(data)\n", |
| 74 |
"data = extrap_data(data)" |
| 75 |
] |
| 76 |
}, |
| 77 |
{ |
| 78 |
"cell_type": "code", |
| 79 |
"execution_count": null, |
| 80 |
"metadata": { |
| 81 |
"scrolled": true |
| 82 |
}, |
| 83 |
"outputs": [], |
| 84 |
"source": [ |
| 85 |
"income_min, income_max = np.min(data['income'].apply(np.min)), np.max(data['income'].apply(np.max))\n", |
| 86 |
"life_exp_min, life_exp_max = np.min(data['lifeExpectancy'].apply(np.min)), np.max(data['lifeExpectancy'].apply(np.max))\n", |
| 87 |
"pop_min, pop_max = np.min(data['population'].apply(np.min)), np.max(data['population'].apply(np.max))" |
| 88 |
] |
| 89 |
}, |
| 90 |
{ |
| 91 |
"cell_type": "code", |
| 92 |
"execution_count": null, |
| 93 |
"metadata": {}, |
| 94 |
"outputs": [], |
| 95 |
"source": [ |
| 96 |
"def get_data(year):\n", |
| 97 |
" year_index = year - 1800\n", |
| 98 |
" income = data['income'].apply(lambda x: x[year_index])\n", |
| 99 |
" life_exp = data['lifeExpectancy'].apply(lambda x: x[year_index])\n", |
| 100 |
" pop = data['population'].apply(lambda x: x[year_index])\n", |
| 101 |
" return income, life_exp, pop" |
| 102 |
] |
| 103 |
}, |
| 104 |
{ |
| 105 |
"cell_type": "code", |
| 106 |
"execution_count": null, |
| 107 |
"metadata": {}, |
| 108 |
"outputs": [], |
| 109 |
"source": [ |
| 110 |
"time_interval = 100\n", |
| 111 |
"fig_layout = widgets.Layout(width='1000px', height='700px', overflow_x='hidden')\n", |
| 112 |
"fig = plt.figure(layout=fig_layout, fig_margin=dict(top=60, bottom=80, left=40, right=20),\n", |
| 113 |
" title='Health and Wealth of Nations', animation_duration=time_interval)\n", |
| 114 |
"\n", |
| 115 |
"plt.scales(scales={'x': bq.LogScale(min=min(200, income_min), max=income_max),\n", |
| 116 |
" 'y': bq.LinearScale(min=life_exp_min, max=life_exp_max),\n", |
| 117 |
" 'color': bq.OrdinalColorScale(domain=data['region'].unique().tolist(), \n", |
| 118 |
" colors=bq.CATEGORY10[:6]),\n", |
| 119 |
" 'size': bq.LinearScale(min=pop_min, max=pop_max)})\n", |
| 120 |
"\n", |
| 121 |
"# add custom x tick values\n", |
| 122 |
"ticks = [2, 4, 6, 8, 10]\n", |
| 123 |
"income_ticks = [t * 100 for t in ticks] + [t * 1000 for t in ticks] + [t * 10000 for t in ticks]\n", |
| 124 |
"\n", |
| 125 |
"# custom axis options\n", |
| 126 |
"axes_options = {'x': dict(label='Income per Capita', label_location='end', \n", |
| 127 |
" label_offset='-2ex', tick_format='~s', \n", |
| 128 |
" tick_values=income_ticks),\n", |
| 129 |
" 'y': dict(label='Life Expectancy', orientation='vertical', \n", |
| 130 |
" side='left', label_location='end', label_offset='-1em'),\n", |
| 131 |
" 'color': dict(label='Region')\n", |
| 132 |
" }\n", |
| 133 |
"\n", |
| 134 |
"tooltip = bq.Tooltip(fields=['name', 'x', 'y'], \n", |
| 135 |
" labels=['Country Name', 'Income per Capita', 'Life Expectancy'])\n", |
| 136 |
"\n", |
| 137 |
"year_label = bq.Label(x=[0.75], y=[0.10], default_size=46, font_weight='bolder', \n", |
| 138 |
" colors=['orange'], text=[str(initial_year)], enable_move=True)\n", |
| 139 |
"\n", |
| 140 |
"# Start with the first year's data\n", |
| 141 |
"cap_income, life_exp, pop = get_data(initial_year)\n", |
| 142 |
"\n", |
| 143 |
"wealth_scat = plt.scatter(cap_income, life_exp, color=data['region'], size=pop,\n", |
| 144 |
" names=data['name'], display_names=False,\n", |
| 145 |
" default_size=20000, tooltip=tooltip, stroke='Black',\n", |
| 146 |
" axes_options=axes_options, unhovered_style={'opacity': 0.5})\n", |
| 147 |
"nation_line = plt.plot(data['income'][0], data['lifeExpectancy'][0],\n", |
| 148 |
" colors=['Gray'], visible=False)\n", |
| 149 |
"\n", |
| 150 |
"# slider for the year\n", |
| 151 |
"year_slider = widgets.IntSlider(min=1800, max=2008, description='Year', value=initial_year)\n", |
| 152 |
"\n", |
| 153 |
"# register callbacks\n", |
| 154 |
"def hover_changed(change):\n", |
| 155 |
" if change.new is not None:\n", |
| 156 |
" nation_line.x = data[data['name'] == wealth_scat.names[change.new]]['income'].values[0]\n", |
| 157 |
" nation_line.y = data[data['name'] == wealth_scat.names[change.new]]['lifeExpectancy'].values[0]\n", |
| 158 |
" nation_line.visible = True\n", |
| 159 |
" else:\n", |
| 160 |
" nation_line.visible = False\n", |
| 161 |
" \n", |
| 162 |
"wealth_scat.observe(hover_changed, 'hovered_point')\n", |
| 163 |
"\n", |
| 164 |
"def year_changed(change):\n", |
| 165 |
" wealth_scat.x, wealth_scat.y, wealth_scat.size = get_data(year_slider.value)\n", |
| 166 |
" year_label.text = [str(year_slider.value)]\n", |
| 167 |
"\n", |
| 168 |
"year_slider.observe(year_changed, 'value')\n", |
| 169 |
"\n", |
| 170 |
"play_button = widgets.Play(min=1800, max=2008, interval=time_interval)\n", |
| 171 |
"widgets.jslink((play_button, 'value'), (year_slider, 'value'))\n", |
| 172 |
"\n", |
| 173 |
"widgets.VBox([widgets.HBox([play_button, year_slider]), fig])" |
| 174 |
] |
| 175 |
} |
| 176 |
], |
| 177 |
"metadata": { |
| 178 |
"kernelspec": { |
| 179 |
"display_name": "Python 3", |
| 180 |
"language": "python", |
| 181 |
"name": "python3" |
| 182 |
}, |
| 183 |
"language_info": { |
| 184 |
"codemirror_mode": { |
| 185 |
"name": "ipython", |
| 186 |
"version": 3 |
| 187 |
}, |
| 188 |
"file_extension": ".py", |
| 189 |
"mimetype": "text/x-python", |
| 190 |
"name": "python", |
| 191 |
"nbconvert_exporter": "python", |
| 192 |
"pygments_lexer": "ipython3", |
| 193 |
"version": "3.8.2" |
| 194 |
} |
| 195 |
}, |
| 196 |
"nbformat": 4, |
| 197 |
"nbformat_minor": 1 |
| 198 |
} |