{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0.3.0.dev\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": { "collapsed": false }, "outputs": [], "source": [ "import json\n", "import pandas as pd\n", "\n", "us_states = os.path.join('data', 'us-states.json')\n", "US_Unemployment_Oct2012 = os.path.join('data', 'US_Unemployment_Oct2012.csv')\n", "\n", "geo_json_data = json.load(open(us_states))\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": { "collapsed": true }, "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": { "collapsed": false }, "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": { "collapsed": false }, "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(['green', 'yellow', 'red'],\n", " vmin=3, vmax=10, index=[3, 4, 8, 10],\n", " caption='step')\n", "\n", "step" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": false }, "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": { "collapsed": false }, "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": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "310" ], "text/plain": [ "" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "linear = cm.LinearColormap(['green', 'yellow', 'red'],\n", " vmin=3, vmax=10)\n", "\n", "linear" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "collapsed": false }, "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": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "0.01.0" ], "text/plain": [ "" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "cm.LinearColormap(['red', 'orange', 'yellow', 'green'],\n", " index=[0, 0.1, 0.9, 1.0])" ] }, { "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": { "collapsed": false }, "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": { "collapsed": false }, "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": { "collapsed": false }, "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": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "0.01.0" ], "text/plain": [ "" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "cm.linear.OrRd" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You can also use them to generate regular `StepColormap`." ] }, { "cell_type": "code", "execution_count": 15, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "0.01.0" ], "text/plain": [ "" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "cm.linear.PuBu.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": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "312" ], "text/plain": [ "" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "cm.linear.YlGn.scale(3, 12)" ] }, { "cell_type": "code", "execution_count": 17, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "5100" ], "text/plain": [ "" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "cm.linear.RdGy.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": { "collapsed": 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", "
BrBg0.01.0
YlOrRd0.01.0
PuRd0.01.0
PiYG0.01.0
RdPu0.01.0
RdYlGn0.01.0
Set10.01.0
Pastel10.01.0
RdBu0.01.0
Accent0.01.0
RdGy0.01.0
OrRd0.01.0
Set30.01.0
Set20.01.0
BuGn0.01.0
Pastel20.01.0
PuOr0.01.0
Spectral0.01.0
Paired0.01.0
BuPu0.01.0
RdYlBu0.01.0
YlOrBr0.01.0
Dark20.01.0
GnBu0.01.0
YlGnBu0.01.0
PRGn0.01.0
PuBu0.01.0
PuBuGn0.01.0
YlGn0.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": { "collapsed": false }, "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.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.5.2" } }, "nbformat": 4, "nbformat_minor": 0 }