{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Set up for JupyterLite\n", "try:\n", " import piplite\n", " await piplite.install(['ipyleaflet', 'branca'])\n", "except ImportError:\n", " pass" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import ipyleaflet\n", "from ipyleaflet import Map, LegendControl\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", "colormap_choice = linear.YlOrRd_04\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_with_NANS.csv\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "data_unemployment = dict(\n", " zip(unemployment[\"State\"].tolist(), unemployment[\"Unemployment\"].tolist())\n", ")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "layer = ipyleaflet.Choropleth(\n", " geo_data=geo_json_data,\n", " choro_data=data_unemployment,\n", " colormap=colormap_choice,\n", " style={\"dashArray\": \"5, 5\"}\n", ")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# To display the colormap bar on the map\n", "from ipywidgets import Output\n", "from ipyleaflet import WidgetControl\n", "\n", "out = Output()\n", "\n", "with out:\n", " colormap = colormap_choice.scale(layer.value_min,layer.value_max)\n", " display(colormap)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "m.add(layer)\n", "m.add(WidgetControl(widget=out, position='topright'))\n", "m" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "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.10.5" } }, "nbformat": 4, "nbformat_minor": 4 }