A Short Guide to Quick and Beautiful Interactive Maps in Folium

Josh Johnson
4 min readOct 11, 2020

--

Photo by Xavi Cabrera on Unsplash

How fun is it to explore? As data scientists, we are all about discovery and interacting with data. Folium allows you and your audience to explore data with interactive maps, and it is quick and simple to set up.

Folium is a python library that allows you to combine the amazing data wrangling libraries of python and the beautiful mapmaking abilities of Leaflet.js. With just a few lines of code in your IPython Jupyter Notebook, you can produce eye-catching interactive maps to help your audience explore your data in a visual and geographical way. While this powerful tool lets you go deep to make really amazing presentations, I’m just going to get you started.

We will be using data from the City of Los Angeles detailing the locations and some information about local farmer’s markets. Crucially it includes latitude and longitude information that we can use to plot markers. We will also use the hours information, organization name, and contact information to create informative markers for our users to explore.

Folium can be downloaded using !pip install folium . We will start by loading our modules (Yay, Pandas!) and downloading and preparing our data.

Now let’s see how easy it is to create a beautiful, interactive map in Folium! In just these two lines of code Folium will create a map centered on the latitude and longitude you in the first argument, and at the zoom scale you pass as the second. Folium maps cannot yet be easily embedded in Medium articles, but you’ll have to take my word for it (or try it yourself) that you can move the map and zoom in and out. Since Folium uses the Leaflet.js map, it will add and remove detail as you zoom, right down to streets and buildings, or up to continents!

With a quick for loop over our Pandas dataframe, we can use the latitude and longitude for each market to add a marker to their location. We can also add a floating tooltip that appears when users hover their mouse over a marker with the argument tooltip. In the below map I used the df['Name'] column in the for loop to add the names of each market to the tooltips.

Now, here I want to offer a warning. Folium maps are memory and processor intensive and more than a few hundred markers tends to bog my system down, especially if they have complicated popups (which I’ll show you how to do next!). I’d love to make maps with thousands of markers, but Folium is not the right library for that. You might try something lighter weight, like Geopandas. (See my previous post!) For this demonstration I mapped 134 farmer’s markets in the L.A. area, and everything ran just fine.

Folium map of L.A. with markers and tooltip

Finally, I want to show you how to make popups to give your users more in-depth information about your plotted markers. Here I’ve included the name, days and hours of operation, a photo, and contact information for the organization that operates each market. Full disclosure, I used a stock image from Unsplash, since I don’t actually have any images of these markets. With Folium, you can use your mighty HTML skills to format your popup with all the options that HTML provides.

There is so much more you can do with Folium! You can add shapes to highlight regions or circles to compare variables such as population. You can also embed Vega graphs with Vincent, or Altair visualizations, and pass any other kind of HTML object to the map markers.

I hope you play around with this surprisingly easy and powerful tool. Please link any great projects you come up with in the comments and checkout the full notebook on nbviewer.

Enjoy!

References:

Folium on Github: https://github.com/python-visualization/folium

City of Los Angeles Datahub: https://geohub.lacity.org/datasets/

Python Visualization: https://python-visualization.github.io/folium/quickstart.html

--

--

Josh Johnson

I'm a data scientist with a background in education. I empower learners to become the folks they want to be.