🤔 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
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
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
-
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
-
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
Note: some mapbox
tiles are available as well, but you need an API key for that