Generate interactive maps using Folium

Chinmay Gaikwad
Jun 29, 2020 · 4 min read

Use the Folium library to visualize geospatial data on the map

Data is much more valuable when it is visualized, a visual summary of information makes it easier to recognise patterns and trends than skimming through thousands of rows on a spreadsheet. However, visualizing data on maps is often a cumbersome and tedious task considering the volume and analysis done on the data.

To simplify the experience of geo-spatial data visualization we can use the rich coding environment of Python. The most appealing thing about Python is that it offers numerous libraries for data visualization with maps, and one of such library is Folium.

Image for post
Image for post

Folium is built on Leaflet.js, a leading open-source JavaScript library for web-friendly interactive maps. It allows both the plotting of data to a map for visualizations as well as passing rich vector/ raster/ HTML visualizations as markers on the map. We will see how Folium can be used with python to create some interactive maps.

Folium is not available by default on the Python environment, we can install the library either with pip or conda.

Generating a simple world map is quite straightforward in Folium. You just have to create a Folium Map object and display it. A beautiful thing about Folium maps is that they are interactive, so you can zoom and zoom out to any region of interest despite the initial zoom level.

Image for post
Image for post

We can focus the map on a specific region by passing the Latitude and Longitude values of that region. For that region, you can also define the initial zoom level into that location when the map is rendered. The higher value of zoom_start sets the map to be more zoomed in at the centre.

Let’s create a map centred around India (Latitude: 20.4937, Longitude: 78.9629) with zoom level 4

Image for post
Image for post

Folium has several maps styles, these are identified by an attribute called tiles. These include Stamen Toner (Black and white), Stamen Terrain (Shading and natural vegetation colours) and Mapbox Bright (Similar to default but few tweaked features). For more details on tiles refer to the documentation link.

Markers are one of the most useful and important attributes of a map. A marker identifies a particular location on the map. Use of interactive markers helps us to understand the data more conveniently.

Let’s create a map with markers superimposed on it displaying the locations. Here we have a dataset of Aurangabad City’s neighbourhoods. We will plot the neighbourhoods on the map with their latitude and longitude values.

Let’s load the neighbourhoods dataset and define it to a dataframe

First, a base map is created by passing Aurangabad’s geo-codes and starting zoom level 11.5. We have used Circle Markers to display the neighbourhoods, also we have added Pop-up Labels to display the name of each neighbourhood when we hover the pointer or click on the marker. We can define custom values to the marker attributes such as radius, colour, fill, opacity etc. Folium has a wide set of markers and labels. To learn more about markers refer to the link.

Upon running the above code pane our map will look like that one below, Carefully observe how markers have been filled with the lighter tone of colour and how the opacity is adjusted to display the locations more attractively.

Image for post
Image for post

We can modify the marker attributes such as radius, colour, fill, opacity etc according to the size and type of map. Folium has a wide set of markers and labels. To learn more about markers refer to the link.

If you want to know more about visualizing time series data on Choropleth maps, Here’s an article ⤵

Thanks for reading!


Thoughts become things!

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store