A Look at Folium

Christos Maglaras
Analytics Vidhya
Published in
5 min readMar 8, 2021

Folium is a feature rich library used to create interactive maps in Python, visualizing geospatial data. Folium is built on leaflet.js, and is often preferred for its much simpler syntax and installation process, while having all the same capabilities. Below we will explore some of the features while creating a map around Chicago.

Noun, a thin leaflike structure, e.g., in some rocks or in the cerebellum of the brain.
!pip install folium
import folium
from folium import plugins
Just a blank map

It's easy to get started with folium, just begin with a pip install and import folium. Also import plugins from folium, as we will be needing third party extensions for some extra functionality later.

Chicago = folium.Map(location=[41.878876, -87.635918],
zoom_start = 13,
control_scale=True)
This one is easier on the eyes

All that's needed to create your first map is the above snippet, passing into Map the coordinates in decimal format, the zoom level (higher is closer), and control_scale as True for overlays to function correctly later.

Chicago = folium.Map(location=[41.878876, -87.635918],
zoom_start = 13,
control_scale = True,
tiles = "OpenStreetMap")
I learned that tooltips disappear in screenshots

Tiles are styles for the map, such as satellite view or greyscale. We will encounter a few more examples of styles further below.

folium.Marker([41.878876, -87.635918], 
popup="Aka Willis Tower",
tooltip='Sears Tower',
icon=folium.Icon(icon="cloud",
color='green')
).add_to(Chicago)
Sears Tower in green, The Bean in red, and Lincoln park purple

One of the most basic features is creating a marker, as shown above. A tooltip can be added to show information while hovering over the pinned location, and a popup will show information in a bubble on click. There are also different icons and colors built in for the markers, as well as the ability to create your own. Remember not to miss the .add_to() at the end to specify what map to add the marker to.

folium.Circle(radius=100,
location=[41.882638554916284,
-87.62330984352772],
popup="Aka Cloud Gate",
tooltip="The Bean",
color="red",
fill=True).add_to(Chicago)
folium.CircleMarker(location=[41.922854499259714,
-87.64905956817513],
radius=65,
popup="Lincoln Park",
color="orange",
fill=True,
fill_color="purple").add_to(Chicago)
When did they change the name again?

The two compliments of the standard marker are the circlemarker and the circle, which are mostly alike with a few differences. Circlemarker is used more as a standard marker to indicate small points on the map, while circle is used to draw attention to larger areas.

Chicago.add_child(folium.LatLngPopup())

LatLngPopup pops up the current latitude and longitude of the cursor wherever the map is clicked.

Chicago.add_child(folium.ClickForMarker(popup="Marked Site"))

ClickForMarker is fairly self explanatory, clicking the map will now drop a pin at that location. Using this along with the LatLngPopup is probably;y not used often as the effect of the popup covers the pin, forcing you to click elsewhere.

folium.raster_layers.TileLayer('Open Street Map').add_to(Chicago)
folium.raster_layers.TileLayer('Stamen Terrain').add_to(Chicago)
folium.raster_layers.TileLayer('Stamen Toner').add_to(Chicago)
folium.raster_layers.TileLayer('Stamen Watercolor').add_to(Chicago)
folium.raster_layers.TileLayer('CartoDB Positron').add_to(Chicago)
folium.raster_layers.TileLayer('CartoDB Dark_Matter').add_to(Chicago)
folium.LayerControl().add_to(Chicago)
Two very different styles

Here we add an additional layer on top of our map to place some interactive GUI elements. Each layer adds an entry to a dropdown allowing you to change map styles much more easily through the map interface.

minimap = plugins.MiniMap(toggle_display=True)
Chicago.add_child(minimap)
Just like a game

A minimap can also be added to show a miniature view of the greater surrounding area, which itself is an interactive way to explore the visualization

plugins.Fullscreen(position='topright').add_to(Chicago)

Another easy to implement feature is a fullscreen mode to maximize the view from the cramped viewing space we have been using so far.

draw = plugins.Draw(export=True)
draw.add_to(Chicago)
display(Chicago)
Might have gone overboard

One of the fun features is the draw plugin, which allows you to annotate the map with point to click lines, polygons, drag radius circles and rectangles, and a few others.

zipmap = folium.Map(location=[41.878876, -87.635918],
zoom_start = 10)
zipcodes = "https://data.cityofchicago.org/api/geospatial/gdcf-axmw?method=export&format=GeoJSON"folium.GeoJson(zipcodes, name="Chicago Zipcodes").add_to(zipmap)

Creating a secondary map as the original is getting quite busy, we import publicly available data provided by the state of Illinois to draw the boundaries of each zipcode in the city. Each state usually provides this information in many formats, .geojson is the format accepted by folium. Visit data.cityofchicago.org for more spatial data such as county or geographical boundaries.

schoolmap = folium.Map(location=[41.878876, -87.635918],
zoom_start = 13)
schools = "https://data.cityofchicago.org/api/geospatial/2mts-wp7t?method=export&format=GeoJSON"folium.GeoJson(schools, name="Chicago Public Schools").add_to(schoolmap)plugins.Fullscreen(position='topright').add_to(schoolmap)
That's more than I expected

Now using a new dataset also provided by the city of Chicago, we can add a marker at the location of every public school within the city. Aulthough this displays the exact position of each school on the map, it is not easy to understand the distribution at a glance. For that we can use a plugin called Heatmap that creates an easy to read density overlay.

import pandas as pdschools_csv = pd.read_csv("Chicago_Public_Schools.csv")
latitude = schools_csv['Lat']
longitude = schools_csv['Long']
schoolheatmap = folium.Map(location=[41.878876,-87.635918],
zoom_start = 10)
folium.plugins.HeatMap(list(zip(latitude,
longitude)),
radius=30,
blur=20).add_to(schoolheatmap)
plugins.Fullscreen(position='topright').add_to(schoolheatmap)
Same data, different parameters

Heatmaps do not take in .geojson files, instead we will use the .csv version hosted right under the .geojson file in the same state government website. For a simple heatmap all that is needed are the coordinates. You often will have to experiment with the different parameters of a heatmap to create a meaningful visualization. One other note is that the current version of folium has a known bug that does not allow you to disable dynamic resizing, so fitting the parameters to the desired zoom level is a must.

Chicago.save(“Population-folium-map.html”)

Finally, folium does one better than screenshots, you can save your map in .HTML format. This allows the interactive version of the map to be shared, so all your hard work doesn't go to waste.

Please refer to Chicago Data for the datasets used and more, as well as Folium for the full documentation on the library.

--

--