Interactive map with Python and Folium

logo of a chart:Map

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:

%%html
<iframe
   src="/interactiveCharts/288_basic_folium_map.html"
   title="Basic map with folium"
   width="790"
   height="600"
>
</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 Cartodb Positron tile:

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

-

Cartodb dark matter

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

-

Open Street Map

map = folium.Map(
    location=[48.85, 2.35], tiles="OpenStreetMap", zoom_start=10)
map
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

Going further

You might be interested in:

Contact & Edit


👋 This document is a work by Yan Holtz. You can contribute on github, send me a feedback on twitter or subscribe to the newsletter to know when new examples are published! 🔥

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