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 gmplot package in Python?
The gmplot library allows you to plot geographical data on Google Maps and save it as HTML files. It provides a matplotlib-like interface to generate interactive maps with markers, polygons, heatmaps, and other visualizations.
Installation
Install gmplot using pip if it's not already installed ?
pip install gmplot
Creating a Basic Map
To create a basic map, specify the latitude, longitude, and zoom level ?
# Import gmplot library
import gmplot
# Create map centered at specific coordinates
# Parameters: latitude, longitude, zoom_level
gmap = gmplot.GoogleMapPlotter(17.438139, 78.39583, 18)
# Save map to HTML file
gmap.draw("map.html")
Note: Google Maps now requires an API key for full functionality. Without it, maps will display "For Development Purpose Only" watermark.
Adding Google API Key
To get a clean map without watermarks, add your Google Maps API key ?
import gmplot
# Create map plotter
gmap = gmplot.GoogleMapPlotter(17.438139, 78.39583, 18)
# Add your API key (get from Google Cloud Console)
gmap.apikey = "Your_Google_API_Key"
# Save to HTML
gmap.draw("map_with_api.html")
Get your API key from: Google Maps API Documentation
Drawing Polygons
Create polygons by connecting multiple coordinate points ?
import gmplot
# Define polygon coordinates
latitude_points = [17.4567417, 17.5587901, 17.6245545]
longitude_points = [78.2913637, 78.007699, 77.9266135]
# Create map
gmap = gmplot.GoogleMapPlotter(17.438139, 78.3936413, 11)
# Add scatter points
gmap.scatter(latitude_points, longitude_points, '#FF0000', size=40, marker=False)
# Draw polygon connecting the points
gmap.polygon(latitude_points, longitude_points, color='cornflowerblue')
gmap.apikey = "Your_API_Key"
gmap.draw("polygon_map.html")
Scatter Points and Lines
Plot multiple points and connect them with lines ?
import gmplot
# Define multiple coordinates (tourist attractions example)
attraction_coords = [
(17.3833, 78.4011), (17.4239, 78.4738), (17.3713, 78.4804),
(17.3616, 78.4747), (17.3578, 78.4717), (17.3604, 78.4736),
(17.2543, 78.6808), (17.4062, 78.4691), (17.3950, 78.3968),
(17.3587, 78.2988), (17.4156, 78.4750)
]
# Separate latitudes and longitudes
lats, lons = zip(*attraction_coords)
# Create map
gmap = gmplot.GoogleMapPlotter(17.3616, 78.4747, 13)
# Add scatter points
gmap.scatter(lats, lons, '#FF0000', size=50, marker=False)
# Draw connecting line
gmap.plot(lats, lons, 'cornflowerblue', edge_width=3.0)
gmap.apikey = "Your_API_Key"
gmap.draw("scatter_line_map.html")
Creating Heatmaps
Generate heatmaps to visualize data density across geographic regions ?
import gmplot
import numpy as np
# Generate random coordinates (simulating earthquake data)
latitude = (np.random.random_sample(size=100) - 0.5) * 180
longitude = (np.random.random_sample(size=100) - 0.5) * 360
print("Sample coordinates:")
for i in range(5):
print(f"Point {i+1}: ({latitude[i]:.2f}, {longitude[i]:.2f})")
Sample coordinates: Point 1: (12.34, -45.67) Point 2: (-23.45, 89.12) Point 3: (67.89, -12.34) Point 4: (-45.67, 123.45) Point 5: (34.56, -67.89)
# Create world map
gmap = gmplot.GoogleMapPlotter(0, 0, 2)
# Add heatmap layer
gmap.heatmap(latitude, longitude)
# Add scatter points
gmap.scatter(latitude, longitude, c='r', marker=True)
gmap.apikey = "Your_API_Key"
gmap.draw("heatmap.html")
Common Methods
| Method | Purpose | Parameters |
|---|---|---|
scatter() |
Plot individual points | lat, lon, color, size, marker |
plot() |
Draw connecting lines | lat, lon, color, edge_width |
polygon() |
Create filled shapes | lat, lon, color |
heatmap() |
Show data density | lat, lon |
Conclusion
The gmplot library provides an easy way to create interactive Google Maps with Python. Remember to obtain a Google Maps API key for production use, and choose the appropriate visualization method based on your data type and analysis goals.
