{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0.8.3+52.g2758dc7.dirty\n" ] } ], "source": [ "import os\n", "import folium\n", "\n", "print(folium.__version__)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Using `folium.colormap`\n", "\n", "**A few examples of how to use `folium.colormap` in choropleths.**\n", "\n", "Let's load a GeoJSON file, and try to choropleth it." ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "import json\n", "import pandas as pd\n", "import requests\n", "\n", "\n", "url = 'https://raw.githubusercontent.com/python-visualization/folium/master/examples/data'\n", "us_states = f'{url}/us-states.json'\n", "US_Unemployment_Oct2012 = f'{url}/US_Unemployment_Oct2012.csv'\n", "\n", "geo_json_data = json.loads(requests.get(us_states).text)\n", "unemployment = pd.read_csv(US_Unemployment_Oct2012)\n", "\n", "unemployment_dict = unemployment.set_index('State')['Unemployment']" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Self-defined\n", "\n", "You can build a choropleth in using a self-defined function.\n", "It has to output an hexadecimal color string of the form `#RRGGBB` or `#RRGGBBAA`." ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "def my_color_function(feature):\n", " \"\"\"Maps low values to green and hugh values to red.\"\"\"\n", " if unemployment_dict[feature['id']] > 6.5:\n", " return '#ff0000'\n", " else:\n", " return '#008000'" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "m = folium.Map([43, -100], tiles='cartodbpositron', zoom_start=4)\n", "\n", "folium.GeoJson(\n", " geo_json_data,\n", " style_function=lambda feature: {\n", " 'fillColor': my_color_function(feature),\n", " 'color': 'black',\n", " 'weight': 2,\n", " 'dashArray': '5, 5'\n", " }\n", ").add_to(m)\n", "\n", "m.save(os.path.join('results', 'Colormaps_0.html'))\n", "\n", "m" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## StepColormap\n", "\n", "But to help you define you colormap, we've embedded `StepColormap` in `folium.colormap`.\n", "\n", "You can simply define the colors you want, and the `index` (*thresholds*) that correspond." ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/html": [ "310" ], "text/plain": [ "" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import branca.colormap as cm\n", "\n", "\n", "step = cm.StepColormap(\n", " ['green', 'yellow', 'red'],\n", " vmin=3, vmax=10,\n", " index=[3, 4, 8, 10],\n", " caption='step'\n", ")\n", "\n", "step" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "m = folium.Map([43, -100], tiles='cartodbpositron', zoom_start=4)\n", "\n", "folium.GeoJson(\n", " geo_json_data,\n", " style_function=lambda feature: {\n", " 'fillColor': step(unemployment_dict[feature['id']]),\n", " 'color': 'black',\n", " 'weight': 2,\n", " 'dashArray': '5, 5'\n", " }\n", ").add_to(m)\n", "\n", "m.save(os.path.join('results', 'Colormaps_1.html'))\n", "\n", "m" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "If you specify no index, colors will be set uniformely." ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/html": [ "0.01.0" ], "text/plain": [ "" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "cm.StepColormap(['r', 'y', 'g', 'c', 'b', 'm'])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## LinearColormap\n", "\n", "But sometimes, you would prefer to have a *continuous* set of colors. This can be done by `LinearColormap`." ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/html": [ "310" ], "text/plain": [ "" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "linear = cm.LinearColormap(\n", " ['green', 'yellow', 'red'],\n", " vmin=3, vmax=10\n", ")\n", "\n", "linear" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "m = folium.Map([43, -100], tiles='cartodbpositron', zoom_start=4)\n", "\n", "folium.GeoJson(\n", " geo_json_data,\n", " style_function=lambda feature: {\n", " 'fillColor': linear(unemployment_dict[feature['id']]),\n", " 'color': 'black',\n", " 'weight': 2,\n", " 'dashArray': '5, 5'\n", " }\n", ").add_to(m)\n", "\n", "m.save(os.path.join('results', 'Colormaps_2.html'))\n", "\n", "m" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Again, you can set the `index` if you want something irregular." ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/html": [ "0.01.0" ], "text/plain": [ "" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "cm.LinearColormap(\n", " ['red', 'orange', 'yellow', 'green'],\n", " index=[0, 0.1, 0.9, 1.0]\n", ")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "If you want to transform a linear map into a *step* one, you can use the method `to_step`." ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/html": [ "3.010.0" ], "text/plain": [ "" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "linear.to_step(6)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You can also use more sophisticated rules to create the thresholds." ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/html": [ "31100" ], "text/plain": [ "" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "linear.to_step(\n", " n=6,\n", " data=[30.6, 50, 51, 52, 53, 54, 55, 60, 70, 100],\n", " method='quantiles',\n", " round_method='int'\n", ")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "And the opposite is also possible with `to_linear`." ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "text/html": [ "310" ], "text/plain": [ "" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "step.to_linear()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Build-in\n", "\n", "For convenience, we provide a (small) set of built-in linear colormaps, in `folium.colormap.linear`." ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "data": { "text/html": [ "0.01.0" ], "text/plain": [ "" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "cm.linear.OrRd_09" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You can also use them to generate regular `StepColormap`." ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "data": { "text/html": [ "0.01.0" ], "text/plain": [ "" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "cm.linear.PuBuGn_09.to_step(12)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Of course, you may need to scale the colormaps to your bounds. This is doable with `.scale`." ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "data": { "text/html": [ "312" ], "text/plain": [ "" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "cm.linear.YlGnBu_09.scale(3, 12)" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "data": { "text/html": [ "5100" ], "text/plain": [ "" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "cm.linear.RdGy_11.to_step(10).scale(5, 100)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "At last, if you want to check them all, simply ask for `linear` in the notebook." ] }, { "cell_type": "code", "execution_count": 18, "metadata": { "scrolled": false }, "outputs": [ { "data": { "text/html": [ "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
viridis0.01.0
Pastel1_030.01.0
Pastel1_050.01.0
Pastel1_040.01.0
Pastel1_070.01.0
YlOrRd_040.01.0
Pastel1_090.01.0
Pastel1_080.01.0
Spectral_070.01.0
RdYlBu_050.01.0
PuBuGn_030.01.0
Set1_080.01.0
PuBuGn_050.01.0
PuBuGn_040.01.0
PuBuGn_070.01.0
PuBuGn_060.01.0
PuBuGn_090.01.0
PuBuGn_080.01.0
YlOrBr_040.01.0
YlOrBr_050.01.0
Set1_070.01.0
YlOrBr_030.01.0
Set1_050.01.0
YlOrRd_030.01.0
PuOr_060.01.0
PuOr_070.01.0
PuOr_040.01.0
PuOr_050.01.0
PuOr_030.01.0
Purples_090.01.0
Set2_060.01.0
RdYlBu_110.01.0
PuOr_080.01.0
PuOr_090.01.0
Paired_030.01.0
RdBu_030.01.0
RdYlBu_100.01.0
Paired_070.01.0
Paired_060.01.0
Paired_050.01.0
Paired_040.01.0
Paired_090.01.0
Paired_080.01.0
RdGy_030.01.0
PiYG_040.01.0
Accent_030.01.0
BuGn_080.01.0
BuGn_090.01.0
BuGn_040.01.0
BuGn_050.01.0
BuGn_060.01.0
BuGn_070.01.0
BuGn_030.01.0
YlGnBu_070.01.0
YlGnBu_060.01.0
YlGnBu_050.01.0
YlGnBu_040.01.0
YlGnBu_030.01.0
RdBu_060.01.0
RdBu_050.01.0
RdBu_040.01.0
Accent_080.01.0
RdBu_090.01.0
RdBu_080.01.0
Set2_040.01.0
YlGnBu_090.01.0
YlGnBu_080.01.0
Blues_080.01.0
Blues_090.01.0
RdPu_090.01.0
RdPu_080.01.0
Set3_070.01.0
Set3_060.01.0
RdPu_050.01.0
RdPu_040.01.0
RdPu_070.01.0
RdPu_060.01.0
Blues_060.01.0
Blues_070.01.0
RdPu_030.01.0
Blues_050.01.0
Paired_100.01.0
Paired_110.01.0
Paired_120.01.0
PuBu_060.01.0
PuBu_070.01.0
PuBu_040.01.0
PuBu_050.01.0
PuRd_050.01.0
PuBu_030.01.0
PuRd_070.01.0
PuRd_060.01.0
PuRd_090.01.0
PuRd_080.01.0
Set2_070.01.0
PuBu_080.01.0
PuBu_090.01.0
RdBu_100.01.0
RdBu_110.01.0
Accent_060.01.0
Set3_030.01.0
Set3_050.01.0
Set3_120.01.0
Set3_100.01.0
Set3_040.01.0
RdGy_110.01.0
RdGy_100.01.0
Set1_030.01.0
Set1_090.01.0
Set3_090.01.0
BuPu_080.01.0
BuPu_090.01.0
RdYlGn_110.01.0
Blues_030.01.0
Set2_050.01.0
BuPu_030.01.0
BuPu_060.01.0
BuPu_070.01.0
BuPu_040.01.0
BuPu_050.01.0
Accent_040.01.0
YlOrRd_050.01.0
YlOrBr_080.01.0
Oranges_080.01.0
Oranges_090.01.0
Oranges_060.01.0
Oranges_070.01.0
Oranges_040.01.0
YlOrBr_090.01.0
Oranges_030.01.0
YlOrBr_060.01.0
Dark2_060.01.0
Blues_040.01.0
YlOrBr_070.01.0
RdYlGn_050.01.0
Set3_080.01.0
YlOrRd_060.01.0
Dark2_030.01.0
Accent_050.01.0
RdYlGn_080.01.0
RdYlGn_090.01.0
PuOr_110.01.0
YlOrRd_070.01.0
Spectral_110.01.0
RdGy_080.01.0
RdGy_090.01.0
RdGy_060.01.0
RdGy_070.01.0
RdGy_040.01.0
RdGy_050.01.0
RdYlGn_040.01.0
PiYG_090.01.0
RdYlGn_060.01.0
RdYlGn_070.01.0
Spectral_040.01.0
Spectral_050.01.0
Spectral_060.01.0
PiYG_080.01.0
Set2_030.01.0
Spectral_030.01.0
Reds_080.01.0
Set1_040.01.0
Spectral_080.01.0
Spectral_090.01.0
Set2_080.01.0
Reds_090.01.0
Greys_070.01.0
Greys_060.01.0
Greys_050.01.0
Greys_040.01.0
Greys_030.01.0
PuOr_100.01.0
Accent_070.01.0
Reds_060.01.0
Greys_090.01.0
Greys_080.01.0
Reds_070.01.0
RdYlBu_080.01.0
RdYlBu_090.01.0
BrBG_090.01.0
BrBG_080.01.0
BrBG_070.01.0
BrBG_060.01.0
BrBG_050.01.0
BrBG_040.01.0
BrBG_030.01.0
PiYG_060.01.0
Reds_030.01.0
Set3_110.01.0
Set1_060.01.0
PuRd_030.01.0
PiYG_070.01.0
RdBu_070.01.0
Pastel1_060.01.0
Spectral_100.01.0
PuRd_040.01.0
OrRd_030.01.0
PiYG_030.01.0
Oranges_050.01.0
OrRd_070.01.0
OrRd_060.01.0
OrRd_050.01.0
OrRd_040.01.0
Reds_040.01.0
Reds_050.01.0
OrRd_090.01.0
OrRd_080.01.0
BrBG_100.01.0
BrBG_110.01.0
PiYG_050.01.0
YlOrRd_080.01.0
GnBu_040.01.0
GnBu_050.01.0
GnBu_060.01.0
GnBu_070.01.0
Purples_080.01.0
GnBu_030.01.0
Purples_060.01.0
Purples_070.01.0
Purples_040.01.0
Purples_050.01.0
GnBu_080.01.0
GnBu_090.01.0
YlOrRd_090.01.0
Purples_030.01.0
RdYlBu_040.01.0
PRGn_090.01.0
PRGn_080.01.0
PRGn_070.01.0
PRGn_060.01.0
PRGn_050.01.0
PRGn_040.01.0
PRGn_030.01.0
RdYlBu_060.01.0
RdYlGn_100.01.0
YlGn_080.01.0
YlGn_090.01.0
RdYlBu_070.01.0
PiYG_100.01.0
PiYG_110.01.0
YlGn_030.01.0
YlGn_040.01.0
YlGn_050.01.0
YlGn_060.01.0
YlGn_070.01.0
Dark2_050.01.0
Dark2_040.01.0
Dark2_070.01.0
Pastel2_030.01.0
Pastel2_040.01.0
Pastel2_050.01.0
Pastel2_060.01.0
Pastel2_070.01.0
Pastel2_080.01.0
RdYlBu_030.01.0
Dark2_080.01.0
RdYlGn_030.01.0
PRGn_110.01.0
Greens_080.01.0
Greens_090.01.0
Greens_060.01.0
Greens_070.01.0
Greens_040.01.0
Greens_050.01.0
PRGn_100.01.0
Greens_030.01.0
\n", " " ], "text/plain": [ "" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "cm.linear" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Draw a `ColorMap` on a map\n", "\n", "By the way, a ColorMap is also a Folium `Element` that you can draw on a map." ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "m = folium.Map(tiles='cartodbpositron')\n", "\n", "colormap = cm.linear.Set1_09.scale(0, 35).to_step(10)\n", "colormap.caption = 'A colormap caption'\n", "m.add_child(colormap)\n", "\n", "m.save(os.path.join('results', 'Colormaps_3.html'))\n", "\n", "m" ] } ], "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": 1 }