{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import ipyleaflet \n", "import json \n", "import pandas as pd\n", "from ipywidgets import link, FloatSlider\n", "from branca.colormap import linear\n", "import random\n", "\n", "geo_json_data = json.load(open('us-states.json')) \n", "m = ipyleaflet.Map(center = (43,-100), zoom = 4)\n", "unemployment = pd.read_csv('US_Unemployment_Oct2012.csv')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "unemployment = dict(zip(unemployment['State'].tolist(), unemployment['Unemployment'].tolist()))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "layer = ipyleaflet.Choropleth(\n", " geo_data=geo_json_data,\n", " choro_data=unemployment,\n", " colormap=linear.YlOrRd_04,\n", " style={'fillOpacity': 0.8, 'dashArray': '5, 5'}\n", ")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "m.add_layer(layer)\n", "m" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# To add callback for style\n", "def compute_style(feature, colormap, choro_data):\n", " return {\n", " 'fillColor': colormap(choro_data),\n", " 'color': 'white',\n", " 'weight': random.randint(1, 3),\n", " }" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "layer.style_callback=compute_style" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "slider = FloatSlider(min=layer.value_min, max=layer.value_max, continuous_update=False)\n", "slider.value = layer.value_min\n", "link((slider, 'value'), (layer, 'value_min'))\n", "slider" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "linear.YlOrBr_04.to_step(10)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "linear.RdBu_11" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "linear.GnBu_09" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "layer.colormap = linear.RdBu_11" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from ipywidgets import Text, HTML\n", "from ipyleaflet import WidgetControl, GeoJSON \n", "\n", "\n", "html = HTML('''\n", "

Name

\n", " Hover over a state\n", "''')\n", "html.layout.margin = '0px 10px 10px 10px'\n", "control = WidgetControl(widget=html, position='topright')\n", "m.add_control(control)\n", "\n", "def update_html(feature, id, **kwargs):\n", " html.value = '''\n", " State name: \n", " {}\\n\n", " {} \n", " '''.format(id, feature['properties']['name'])\n", "\n", "layer.on_hover(update_html)" ] } ], "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.8.2" } }, "nbformat": 4, "nbformat_minor": 2 }