Folium-A python library used for creating interactive leaflet maps¶
Folium is a powerful Python library used for creating interactive Leaflet maps. It allows you to visualize spatial data in Python and is commonly used for geospatial analysis and data visualization. Folium was built on Leaflet.js which is a JavaScript library. So Folium basically does python based map-creation but uses leaflet under a hood. It enables both the binding of data to a map for choropleth visualizations as well as passing rich vector/raster/HTML visualizations as markers on the map.
In this blog, we will explore:
✅ How to install and set up Folium
✅ Creating a basic interactive map
✅ Adding markers and popups
✅ Visualizing data using heatmaps and choropleth maps
Installation¶
Folium can be installed using pip either in command prompt or directly in jupyter notebook
pip install folium
After installation, you need to import the library to jupyter notebook as shown below
import folium
Folium has the following dependencies, all of which are installed automatically with the above installation commands:
branca
Jinja2
Numpy
Requests
Additional packages may be necessary for some functionality. It will say so in the documentation where that’s the case.
Key features¶
1. Easy map creation¶
- Create interactive Leaflet.js-based maps
- Can add custom markers at specific locations
- Attach popups and tooltips with extra
- Can draw shapes
- Can visualize heatmaps and choropleths
- Can also create minimaps as well as full screen maps
2. Multiple Tile Styles¶
It supports multiple base styles
Some are:
- "OpenStreetMap"
- "Stamen Terrain"
- "Stamen Toner"
- "CartoDB Positron"
3. GeoJSON and Shapefile Support¶
Supports GeoJSON & Shapefile formats for custom boundaries and regions
4. Save and Share Maps as HTML¶
Maps can be saved as HTML files and embedded in websites
For better understanding of these key features, Let us go through some codes and explore the visualizations
Examples¶
Now let's create a basic map of IIT Gandhinagar and see how amazingly a small piece of code shows visualization.
Basic map¶
import folium
# Create a map centered at IIT Gandhinagar
m = folium.Map(location=[23.2156, 72.6842], zoom_start=14,tiles="OpenStreetMap")
#location=[latitude, longitude]
#if tiles is not explicitly mentioned, it gets set to "open street map" by default.
#Zoom_start sets the default zoom level
# Save the map as an HTML file
m.save("basic_map.html")
# Display the map in Jupyter Notebook
m
Adding marker¶
import folium
# Create a map centered at IIT Gandhinagar
m = folium.Map(location=[23.2156, 72.6842], zoom_start=13)
# Add a marker
folium.Marker([23.2156, 72.6842], popup="IIT Gandhinagar", tooltip="Click me!").add_to(m)
# Save to HTML
m.save("map.html")
m
Intrestingly, we can also add shapes to indicate certain special places. We can add circles, polygons. We can also draw lines to show routes etc.
Enclosing an area by circle¶
# Create a map centered at IIT Gandhinagar
m = folium.Map(location=[23.2156, 72.6842], zoom_start=14)
# Add a circle to represent an area
folium.Circle(
location=[23.2156, 72.6842], # Center of the circle
radius=500, # Radius in meters
color="blue",
fill=True,
fill_color="lightblue",
popup="500m radius around IITGN"
).add_to(m)
# Save map
m.save("circle_map.html")
m
Enclosing an area by polygon¶
import folium
folium.Polygon(
locations=[[23.2060, 72.6720], [23.2080, 72.6750], [23.2040, 72.6770]],
color="red",
fill=True,
fill_color="pink",
popup="Custom Polygon"
).add_to(m)
#used 3 positions for triangle. we can use n locations to draw n sided polygon
m.save("polygon_map.html")
m
Drawing lines¶
folium.PolyLine(
locations=[[23.2156, 72.6842], [23.2180, 72.6890], [23.2200, 72.6920]],
color="green",
weight=5,
popup="Pathway"
).add_to(m)
#creates a green pathway joining multiple points
m.save("line_map.html")
m
We can also visualize heatmaps using folium. A heatmap is a visualization where colors represent the density of data points.
Visualizing Heatmaps¶
import folium
from folium.plugins import HeatMap
# Create a map centered at IIT Gandhinagar
m = folium.Map(location=[23.2156, 72.6842], zoom_start=14)
# Sample heatmap data (latitude, longitude, intensity)
heat_data = [
[23.2156, 72.6842, 1], # High intensity
[23.2160, 72.6800, 0.5], # Medium intensity
[23.2170, 72.6830, 0.8], # Higher intensity
]
# Add heatmap layer
HeatMap(heat_data).add_to(m)
# Save and display map
m.save("heatmap.html")
m
We can also create choropleth maps using folium. A choropleth map is a thematic map where regions are colored based on data values (e.g., population, income).
Creating Choropleth map¶
Firstly,Let us import states.json file
import requests
geo_json_data = requests.get(
"https://raw.githubusercontent.com/python-visualization/folium-example-data/main/us_states.json"
).json()
Now,let us make a choropleth map from that file
m = folium.Map([43, -100], zoom_start=4)
folium.GeoJson(geo_json_data, zoom_on_click=True).add_to(m)
#Zooms in the part where you click
m
Practical applications¶
Folium is versatile and powerful for mapping, analysis, and data visualization. It finds applications in urban planning, disaster response, business intelligence, and scientific research. It is used for:
- Geospatial Data Visualization
- Route & Navigation Planning
- Traffic & Infrastructure Analysis
- Disaster Management & Emergency Response
- Business & Market Analysis
- Agriculture & Environmental Monitoring
- Election & Census Data Visualization
Conclusion¶
Maps tell stories, and with Folium, creating interactive maps becomes effortless and fun. Whether you’re just getting started with geospatial data or looking to build something more advanced, Folium gives you the tools to bring location-based data to life with just a few lines of code.
In this blog, we walked through how to set up Folium, add markers, change map styles, and visualize data with heatmaps and choropleth maps. Along the way, we saw how simple tweaks can turn a plain map into something rich with insights.
What We Learned
- How to create and customize interactive maps.
- How to add markers, popups, and different tile layers.
- How to work with data and visualize it effectively using Folium.
- Real-world applications where Folium can be used, from city planning to tracking trends. Folium makes mapping easy, but the best way to learn is to experiment. Try it with your own data, play around with different features, and see what stories your maps can tell.
If you’re curious to explore more, check out the Folium documentation and start building!
For further reading¶
Folium documentation- https://python-visualization.github.io/folium/
Leaflet(Java script library)- https://leafletjs.com/