Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Plotting Google Map using folium package?
Folium is a powerful Python library that allows you to create interactive Leaflet maps. These interactive maps are ideal for building dashboards and visualizing geospatial data.
Installation
Installing folium is straightforward using pip ?
pip install folium
Creating a Basic Map
Let's start by creating a simple map centered on Hyderabad, India ?
import folium
# Create a map centered at latitude 17.3616, longitude 78.4747 (Hyderabad)
# zoom_start controls initial zoom level (higher = more zoomed in)
map_obj = folium.Map(location=[17.3616, 78.4747], zoom_start=4, tiles='Stamen Terrain')
# Save the map as HTML file
map_obj.save('basic_map.html')
print("Basic map created and saved as 'basic_map.html'")
Basic map created and saved as 'basic_map.html'
Adding GeoJSON Data
You can add geographical features using GeoJSON format. Here's how to plot random points ?
import folium
import numpy as np
from folium import features
# Generate random coordinates around a central point
N = 100
lons = 5 - np.random.normal(size=N)
lats = 48 - np.random.normal(size=N)
# Create GeoJSON data structure
geojson_data = {
'type': 'FeatureCollection',
'features': [
{
'type': 'Feature',
'geometry': {
'type': 'MultiPoint',
'coordinates': [[lon, lat] for (lat, lon) in zip(lats, lons)],
},
'properties': {'name': 'Random Points'}
},
],
}
# Create map and add GeoJSON layer
map_geo = folium.Map([17.3616, 78.4747], zoom_start=10)
map_geo.add_child(features.GeoJson(geojson_data))
map_geo.save('geojson_map.html')
print("GeoJSON map created successfully")
GeoJSON map created successfully
Adding Markers with Popups and Icons
Markers help highlight specific locations. You can customize them with icons and popup messages ?
import folium
from folium import features
# Create a map centered on Charminar
map_marker = folium.Map([17.3616, 78.4747], zoom_start=15)
# Create marker with custom icon and popup
marker = features.Marker([17.3616, 78.4747])
popup = folium.Popup('Charminar - Historic Monument')
icon = features.Icon(color='red')
# Add components to marker
marker.add_child(icon)
marker.add_child(popup)
# Add marker to map
map_marker.add_child(marker)
map_marker.save('marker_map.html')
print("Marker map with popup created")
Marker map with popup created
Creating Interactive Charts in Popups
For advanced visualizations, you can embed charts in popups using Vincent library ?
# First install vincent: pip install vincent
import folium
from folium import features
import numpy as np
import json
import vincent
# Generate sample data for scatter plot
N = 50
scatter_data = {
'x': np.random.uniform(size=(N,)),
'y': np.random.uniform(size=(N,)),
}
# Create Vincent scatter plot
scatter = vincent.Scatter(scatter_data, iter_idx='x', height=200, width=300)
chart_data = json.loads(scatter.to_json())
# Create map with embedded chart
map_chart = folium.Map([17.3616, 78.4747], zoom_start=10)
marker = features.Marker([17.3616, 78.4747])
popup = folium.Popup('Data Visualization')
vega_chart = features.Vega(chart_data, width='100%', height='100%')
# Build the marker hierarchy
marker.add_child(popup)
popup.add_child(vega_chart)
map_chart.add_child(marker)
map_chart.save('chart_map.html')
print("Map with embedded chart created")
Common Map Tile Options
| Tile Style | Description | Best For |
|---|---|---|
| OpenStreetMap | Default street map | General purpose |
| Stamen Terrain | Terrain with elevation | Geographic features |
| CartoDB positron | Light, clean design | Data visualization |
| CartoDB dark_matter | Dark theme | Night mode dashboards |
Conclusion
Folium makes it easy to create interactive maps in Python with support for markers, popups, GeoJSON data, and embedded visualizations. Use it for geospatial analysis, dashboard building, and location-based data storytelling.
