Folium: Create Interactive Leaflet Maps
By: Himanshu Sharma
Tags: Data Visualization, Maps, Data Science, Python, Dashboard

Folium: Create Interactive Leaflet Maps

In this article, we will explore Folium, a Python library which makes it easy to visualize data that’s been manipulated in Python on an interactive leaflet map.

The maps created using folium are highly interactive which makes it even more useful for dashboard building. So let’s start exploring Folium and learn on our way of exploring it.

Installing Folium

We can install folium by running the following given below command in our command prompt.

pip install folium

Creating a world map using folium

After installing folium we need to import it in our Jupiter notebook to start working. Folium is an easy to understand library by which you can create highly interactive and visually appealing maps in just a few lines of code.

Here we will learn how we can use it to create a world map. For this, we just need to call the Map() function. The code given below will help you understand this better.

#importing folium library

import folium # calling Map() function

world_map = folium.Map() #displaying world map

world_map

Map using Folium with just one line of code.

The map created above is interactive i.e. you can actually zoom-in and zoom-out of the map using the ‘+’ and ‘-’ or by just using the cursor of your mouse.

We can pass the ‘location’ argument which contains the longitude and latitude of a particular location to Map() function to display the map of the desired location. For example, you can see the code below I used to create the map of Russia using its coordinates. Here I have also passed an attribute named ‘zoom_start’ which already zoom-in accordingly.

mah_map = folium.Map(location=[61.5240, 105.3188], zoom_start=3)

mah_map

Russia’s Map created using Folium

Further, you can use folium for creating different types of maps. Some of them I have explained below.

Stamen Toner Maps

These are the high contrast Black & White maps. They are perfect for data mashups and exploring river meanders and coastal zones.

For creating this we just need to add an attribute to Map() function named ‘tiles’ and set it to Stamen Toner. The code below is used to create a Stamen Toner map of India.

india_map = folium.Map(location=[20.5937, 78.9629 ], zoom_start=4, tiles='Stamen Toner') india_map

Stamen Toner Map of India.

Stamen Terrain Maps

These maps show hill shading and natural vegetation colors. They also showcase the advanced labeling and linework generalization of dual-carriageway roads.

For creating this we need to set the value of attribute tiles as Stamen Terrain. The below code will produce the Stamen Terrain map of India.

india_map = folium.Map(location=[20.5937, 78.9629 ], zoom_start=4, tiles='Stamen Terrain') india_map

Stamen Terrain Map of India.

Other than this there are many more tile options like ‘Mapbox Bright’ etc. that you can explore and learn.

Folium also has a marker function that marks the desired location of your choice of given coordinates. We can also select the icon for the marker.

Below Given code is used to create a map and displaying the marker on the desired location.

my_map = folium.Map(location=[20.5937, 78.9629], zoom_start=4, tiles='Stamen Terrain') folium.Marker([28.73158, 77.13267], popup='MyLocation',

marker_icon='cloud').add_to(my_map)

my_map

Marker showing pop-up.

Conclusion

In this article, we explored a beautiful library Folium use to create leaflet maps that are interactive and visually appealing. We can use these maps in our dashboards or as needed. Folium is easy to understand the library and creates a map in just one line of code. This article is just a basic understanding of folium, go ahead, and explore folium for many more features.

Before You Go

Thanks for reading! If you want to get in touch with me, feel free to reach me on hmix13@gmail.com or my LinkedIn Profile. You can also view the code and data I have used here in my Github.