{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Folium examples\n", "\n", "##### Germain Salvato Vallverdu [germain.vallverdu@gmail.com](germain.vallverdu@gmail.com)\n", "\n", "This notebook shows simple examples of [Folium package](https://folium.readthedocs.io/en/latest/quickstart.html#getting-started) in order to draw marker on a map.\n", "\n", "Colors from [flatui colors](https://flatuicolors.com/)." ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": true }, "outputs": [], "source": [ "import folium" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Basic marker\n", "\n", "Basic marker with only position and popup message" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "carte = folium.Map(location=[45.5236, -122.6750], zoom_start=12)\n", "marker = folium.Marker([45.5, -122.7], popup='Un marker')\n", "marker.add_to(carte)\n", "carte" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Circle Marker\n", "\n", "Help on CircleMarker object :\n", "\n", " Init signature: folium.CircleMarker(location, radius=500, color='black', fill_color='black', fill_opacity=0.6, popup=None)\n", " Docstring: \n", " Creates a CircleMarker object for plotting on a Map.\n", "\n", " Parameters\n", " ----------\n", " location: tuple or list, default None\n", " Latitude and Longitude of Marker (Northing, Easting)\n", " radius: int\n", " The radius of the circle in pixels.\n", " color: str, default 'black'\n", " The color of the marker's edge in a HTML-compatible format.\n", " fill_color: str, default 'black'\n", " The fill color of the marker in a HTML-compatible format.\n", " fill_opacity: float, default à.6\n", " The fill opacity of the marker, between 0. and 1.\n", " popup: string or folium.Popup, default None\n", " Input text or visualization for object." ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "carte = folium.Map(location=[45.5236, -122.6750], zoom_start=12)\n", "circle = folium.CircleMarker(\n", " [45.5, -122.7],\n", " radius=1000, \n", " popup='Un cercle', \n", " color=\"#e74c3c\", # rouge\n", " fill_color=\"#27ae60\", # vert\n", " fill_opacity=0.9\n", ")\n", "circle.add_to(carte)\n", "carte" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Icon Marker\n", "\n", "Help on Icon object :\n", "\n", " Init signature: folium.Icon(color='blue', icon_color='white', icon='info-sign', angle=0, prefix='glyphicon')\n", " Docstring: \n", " Creates an Icon object that will be rendered\n", " using Leaflet.awesome-markers.\n", "\n", " Parameters\n", " ----------\n", " color : str, default 'blue'\n", " The color of the marker. You can use:\n", "\n", " ['red', 'blue', 'green', 'purple', 'orange', 'darkred',\n", " 'lightred', 'beige', 'darkblue', 'darkgreen', 'cadetblue',\n", " 'darkpurple', 'white', 'pink', 'lightblue', 'lightgreen',\n", " 'gray', 'black', 'lightgray']\n", "\n", " icon_color : str, default 'white'\n", " The color of the drawing on the marker. You can use colors above,\n", " or an html color code.\n", " icon : str, default 'info-sign'\n", " The name of the marker sign.\n", " See Font-Awesome website to choose yours.\n", " Warning : depending on the icon you choose you may need to adapt\n", " the `prefix` as well.\n", " angle : int, default 0\n", " The icon will be rotated by this amount of degrees.\n", " prefix : str, default 'glyphicon'\n", " The prefix states the source of the icon. 'fa' for font-awesome or\n", " 'glyphicon' for bootstrap 3.\n", "\n", "Icons can be drawn from bootstrap or font-awesome glyphs. You can chose custum color for the icon but the maker\n", "color must be chosen in the provided list (see doc above).\n", "\n", "Web sites for glyphs :\n", "\n", "* bootstrap icons : [bootstrap glyphicons](http://getbootstrap.com/components/#glyphicons)\n", "* font-awesome icons : [font-awesome](http://fontawesome.io/icons/)" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "carte = folium.Map(location=[45.5236, -122.6750], zoom_start=12)\n", "\n", "# add firt marker with bootstrap icon\n", "icone1 = folium.Icon(icon=\"asterisk\", icon_color=\"#9b59b6\", color=\"lightblue\")\n", "marker1 = folium.Marker([45.5, -122.7], popup='Un icone', icon=icone1)\n", "marker1.add_to(carte)\n", "\n", "# add second marker with font-awesome icon\n", "icone1 = folium.Icon(icon=\"globe\", icon_color=\"#e67e22\", color=\"lightgreen\", prefix=\"fa\")\n", "marker1 = folium.Marker([45.5, -122.6], popup='Un icone', icon=icone1)\n", "marker1.add_to(carte)\n", "\n", "carte" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## RGB(A) to HEX colors\n", "\n", "How can I convert RGB or RGBA colors to hex colors ?\n", "\n", "You can use an internal function of matplotlib to convert a RGB or RGB(A) color to HEX color." ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": true }, "outputs": [], "source": [ "import matplotlib\n", "import matplotlib.pyplot as plt" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "(0.0, 0.086274509803921567, 0.95686274509803926, 1.0)" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "color = plt.cm.winter(22)\n", "color" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "'#0016f4'" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "matplotlib.colors.rgb2hex(color)" ] } ], "metadata": { "anaconda-cloud": {}, "kernelspec": { "display_name": "Python [conda env:geopandas]", "language": "python", "name": "conda-env-geopandas-py" }, "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": 1 }