Generate interactive maps using Folium

Chinmay Gaikwad
ChiGa
Published in
4 min readJun 29, 2020

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.

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.

Plotting a basic map

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.

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

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.

Maps with Markers

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.

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!

--

--