Using Folium in Python one can display maps. Folium can be installed using pip install.
Folium make use of the leaflet.js library. In a previous post I already demonstrated how to e.g. plot markers and heatmaps onto maps in R, using the Leaflet R package.
In this post I give a brief introduction to Folium in Python.
Below is an example creating a simple map using Folium in Python:
# import folium in Python import folium # use the Map function from folium to generate a map folium.Map()
Using the location parameter one can feed a center point to the Map function – in form of a list with latitude and longitude coordinates:
# calling Map() function using the location parameter folium.Map(location=[45.0,45.0])
The function supports additional parameters, such as e.g. the zoom level:
# making use of start_zoom parameter folium.Map(location=[45.0,45.0], zoom_start = 3)
The “tile” parameter is a parameter enabling the user to select a certain map style. Below I e.g. use the “Stamen Toner” map tiles:
# making use of the tile parameter in Map() function folium.Map(location=[45.0,45.0], zoom_start = 5, tiles= "Stamen Toner")
Using folium, i.e. leaflet.js library, one can create markers similar to the ones demonstrated in R.
For this, create a map with folium() – and add markers to it createde with the .Marker() function. An example is displayed below:
# create map object with folium.Map() mapObject = folium.Map(location = [45,45], zoom_start = 5) # create markers with .Marker markerObjects = folium.Marker(location= [45.5,44.5], popup = "This is a marker!") # add marker to map markerObjects.add_to(mapObject) # display map with marker mapObject
Below I add another marker with a red cloud icon:
# creating additional marker in red redMarkerObject = folium.Marker(location = [43,43], icon=folium.Icon(color="red", icon="cloud")) # add marker in red to map redMarkerObject.add_to(mapObject) # display map with additional markers mapObject
Instead of marker icons such as the ones displayed above one could also add e.g. circles, heatmaps etc. I will post additional coding examples in Python, using Folium.
Data scientist focusing on simulation, optimization and modeling in R, SQL, VBA and Python
Leave a Reply