{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import os\n", "import json\n", "\n", "import numpy as np\n", "import pandas as pd\n", "\n", "from ipywidgets import Dropdown\n", "\n", "from bqplot import Lines, Figure, LinearScale, DateScale, Axis\n", "\n", "from ipyleaflet import Map, GeoJSON, WidgetControl" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "data = pd.read_json(os.path.abspath('nations.json'))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def clean_data(data):\n", " for column in ['income', 'lifeExpectancy', 'population']:\n", " data = data.drop(data[data[column].apply(len) <= 4].index)\n", " return data\n", "\n", "def extrap_interp(data):\n", " data = np.array(data)\n", " x_range = np.arange(1800, 2009, 1.)\n", " y_range = np.interp(x_range, data[:, 0], data[:, 1])\n", " return y_range\n", "\n", "def extrap_data(data):\n", " for column in ['income', 'lifeExpectancy', 'population']:\n", " data[column] = data[column].apply(extrap_interp)\n", " return data" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "data = clean_data(data)\n", "data = extrap_data(data)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "data" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "date_start = pd.datetime(1800, 12, 31)\n", "date_end = pd.datetime(2009, 12, 31)\n", "\n", "date_scale = DateScale(min=date_start, max=date_end)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "date_data = pd.date_range(start=date_start, end=date_end, freq='A', normalize=True)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "country_name = 'Angola'\n", "data_name = 'income'\n", "\n", "x_data = data[data.name == country_name][data_name].values[0]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "x_scale = LinearScale()\n", "\n", "lines = Lines(x=date_data, y=x_data, scales={'x': date_scale, 'y': x_scale})\n", "\n", "ax_x = Axis(label='Year', scale=date_scale, num_ticks=10, tick_format='%Y')\n", "ax_y = Axis(label=data_name.capitalize(), scale=x_scale, orientation='vertical', side='left')\n", "\n", "figure = Figure(axes=[ax_x, ax_y], title=country_name, marks=[lines], animation_duration=500,\n", " layout={'max_height': '250px', 'max_width': '400px'})\n", "figure" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def update_figure(country_name, data_name):\n", " lines.y = data[data.name == country_name][data_name].values[0]\n", " ax_y.label = data_name.capitalize()\n", " figure.title = country_name" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "country_name = 'Benin'\n", "data_name = 'income'\n", "\n", "update_figure(country_name, data_name)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "country_name = 'Angola'\n", "data_name = 'population'\n", "\n", "update_figure(country_name, data_name)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "with open('./countries.geo.json') as f:\n", " countries = json.load(f)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "m = Map(zoom=3)\n", "\n", "geo = GeoJSON(data=countries, style={'fillColor': 'white', 'weight': 0.5}, hover_style={'fillColor': '#1f77b4'}, name='Countries')\n", "m.add_layer(geo)\n", "\n", "m" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "widget_control1 = WidgetControl(widget=figure, position='bottomright')\n", "\n", "m.add_control(widget_control1)\n", "\n", "def on_hover(event, feature, **kwargs):\n", " global country_name\n", "\n", " country_name = feature['properties']['name']\n", " update_figure(country_name, data_name)\n", "\n", "geo.on_hover(on_hover)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "dropdown = Dropdown(\n", " options=['income', 'population', 'lifeExpectancy'],\n", " value=data_name,\n", " description='Plotting:'\n", ")\n", "\n", "def on_click(change):\n", " global data_name\n", "\n", " data_name = change['new']\n", " update_figure(country_name, data_name)\n", "\n", "dropdown.observe(on_click, 'value')\n", "\n", "widget_control2 = WidgetControl(widget=dropdown, position='bottomleft')\n", "\n", "m.add_control(widget_control2)" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.7.3" } }, "nbformat": 4, "nbformat_minor": 4 }