{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# How to draw a *scatter* plot with folium" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": true }, "outputs": [], "source": [ "import numpy as np\n", "import sys\n", "sys.path.insert(0, 'folium')\n", "sys.path.insert(0, 'branca')\n", "\n", "import branca\n", "import folium" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We create a sample of data thanks to `numpy`." ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [], "source": [ "n = 100\n", "lats = np.random.uniform(38, 53, n)\n", "lngs = np.random.uniform(-17, 23, n)\n", "sizes = np.random.uniform(2, 20, n)\n", "colors = np.random.uniform(0, 50, n)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We create a colormap thanks to `branca`. You can also create your own function or use `matplotlib`." ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "#ffff00\n" ] }, { "data": { "text/html": [ "050" ], "text/plain": [ "" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "cm = branca.colormap.LinearColormap(['green', 'yellow', 'red'], vmin=0, vmax=50)\n", "print(cm(25))\n", "cm" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We create a `FeatureGroup` with all the points." ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": true }, "outputs": [], "source": [ "f = folium.map.FeatureGroup()\n", "\n", "for lat, lng, size, color in zip(lats, lngs, sizes, colors):\n", " f.add_child(\n", " folium.features.CircleMarker(\n", " [lat, lng],\n", " radius=size,\n", " color=None,\n", " fill_color=cm(color),\n", " fill_opacity=0.6)\n", " )" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "And draw the map !" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "m = folium.Map([46,4], zoom_start=5, tiles='mapquestopen')\n", "m.add_child(f)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "That's all folks !" ] } ], "metadata": { "kernelspec": { "display_name": "prototyping", "language": "python", "name": "prototyping" }, "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.1" } }, "nbformat": 4, "nbformat_minor": 0 }