Interactive map with Python and Folium


Folium is a python library wrapping the leaflet.js javascript library. It allows to build interactive maps with several kind of background tiles as shown in this blogpost.

🤔 What is Folium

Folium is a python library allowing to call the Leaflet.js Javascript library. It allows you to manipulate your data with python and map them using the power of leaflet! It is really easy to call a map using this library.

Folium can be installed with pip using the following:

pip install folium

👍 Most basic map

To create a map with Folium, simply pass the coordinates of the location you're interested in to the Map() function:

# Import the folium library
import folium

# Build the default map for a specific location
map = folium.Map(location=[43.61092, 3.87723])
map
Make this Notebook Trusted to load map: File -> Trust Notebook

Welcome to Montpellier, south of France, where I live most of the time 😊!

💾 Save as html

If you're not working in a jupyter notebook, you probably want to export the map to a standalone .html file. This is done thanks to the save function:

# Save the map to a specific location in my laptop
map.save('../../static/interactiveCharts/288_basic_folium_map.html')

You can now embed this page in any html document using an iframe. Open your .html document and add this somewhere: <iframe src="/interactiveCharts/288_basic_folium_map.html" title="Basic map with folium" style={{ border: "none", width: '800px', height: '300px' }}></iframe>

🌈 Change tile

The folium library comes with several options for the background tile. The background tile is set thanks to the tiles parameter. For instance, let's visit Paris with a Stamen Toner tile:

tonerMap = folium.Map(location=[48.85, 2.35], tiles="Stamen Toner", zoom_start=10)
tonerMap
Make this Notebook Trusted to load map: File -> Trust Notebook

Available tiles

Here is an overview of the tiles Folium comes with.

Open Street Map

osmMap = folium.Map(location=[48.85, 2.35], tiles="OpenStreetMap", zoom_start=10)
osmMap.save('../../static/interactiveCharts/288_basic_folium_map_osm.html')
osmMap
Make this Notebook Trusted to load map: File -> Trust Notebook

-

Stamen Terrain

terrainMap = folium.Map(location=[48.85, 2.35], tiles="Stamen Terrain", zoom_start=10)
terrainMap.save('../../static/interactiveCharts/288_basic_folium_map_terrain.html')
terrainMap
Make this Notebook Trusted to load map: File -> Trust Notebook

-

Stamen Toner

tonerMap = folium.Map(location=[48.85, 2.35], tiles="Stamen Toner", zoom_start=10)
tonerMap.save('../../static/interactiveCharts/288_basic_folium_map_toner.html')
tonerMap
Make this Notebook Trusted to load map: File -> Trust Notebook

Note: some mapbox tiles are available as well, but you need an API key for that

Map

Choropleth

Hexbin

Cartogram

Connection

Bubble

Contact & Edit

👋 This document is a work by Yan Holtz. Any feedback is highly encouraged. You can fill an issue on Github, drop me a message onTwitter, or send an email pasting yan.holtz.data with gmail.com.

This page is just a jupyter notebook, you can edit it here. Please help me making this website better 🙏!

Violin

Density

Histogram

Boxplot

Ridgeline

Scatterplot

Heatmap

Correlogram

Bubble

Connected Scatter

2D Density

Barplot

Spider / Radar

Wordcloud

Parallel

Lollipop

Circular Barplot

Treemap

Venn Diagram

Donut

Pie Chart

Dendrogram

Circular Packing

Line chart

Area chart

Stacked Area

Streamgraph

Map

Choropleth

Hexbin

Cartogram

Connection

Bubble

Chord Diagram

Network

Sankey

Arc Diagram

Edge Bundling

Colors

Interactivity

Animation with python

Animation

Cheat sheets

Caveats

3D