gmplot
gmplot is a Python library providing a matplotlib-like interface to plot geographical data on Google Maps. It allows users to easily add markers, heatmaps, polygons, and other visualizations to generate interactive HTML maps. The library is actively maintained; its current version, 1.4.1, includes features like draggable markers and ground overlays.
Common errors
-
AttributeError: module 'gmplot' has no attribute 'GoogleMapPlotter'
cause This usually indicates an outdated `gmplot` installation, a local file named `gmplot.py` shadowing the actual library, or an incorrect import where `gmplot` is treated as the class itself.fix1. Ensure you have the latest `gmplot`: `pip install --upgrade gmplot`. 2. Check if there's a file named `gmplot.py` in your working directory and rename it. 3. Correct the import and instantiation to `import gmplot; gmap = gmplot.GoogleMapPlotter(lat, lon, zoom)`. -
TypeError: 'module' object is not callable
cause Attempting to call the `gmplot` module directly as a constructor, instead of using the `GoogleMapPlotter` class within it.fixThe correct way to create a map object is `gmap = gmplot.GoogleMapPlotter(latitude, longitude, zoom_level)`. -
Map displays 'For Development Purposes Only' watermark
cause A Google Maps API key is required for production use and to remove the watermark, but it has not been provided or is invalid.fixObtain a valid Google Maps API key from the Google Cloud Console and pass it as the `apikey` parameter during `GoogleMapPlotter` instantiation: `gmap = gmplot.GoogleMapPlotter(..., apikey='YOUR_API_KEY')`. -
TypeError: argument of type 'float' is not iterable (when using scatter with size/color)
cause The `size` or `color` parameter in `scatter()` was passed an iterable (e.g., a tuple or list) when the method expected a single float for `size` or a single string for `color` to apply uniformly, or vice-versa.fixIf you want a uniform style, pass a single value (e.g., `size=40`, `color='red'`). If you want different styles per point, ensure the list of styles has the same length as your latitude/longitude lists and that the type for each element matches the expected parameter type (e.g., `size=[20, 30, 40]`). Alternatively, loop and plot each point individually if complex individual styling is needed.
Warnings
- gotcha Using Google Maps without an API key will result in a 'For Development Purposes Only' watermark on your generated maps.
- gotcha The `GoogleMapPlotter` class must be instantiated as `gmplot.GoogleMapPlotter(...)`. Trying to call `gmplot()` directly will result in a `TypeError: 'module' object is not callable`.
- gotcha When using `scatter()` or other plotting methods, ensure that iterable arguments like `color`, `size`, or `marker` match the length of `latitudes` and `longitudes`, or provide a single value to apply uniformly.
- gotcha The `plot()` method is used for drawing lines or paths, while `polygon()` is used for drawing filled, closed shapes. Misusing them can lead to unexpected visual output.
- deprecated Python 2 support has been dropped. gmplot is primarily designed and tested for Python 3 environments.
Install
-
pip install gmplot
Imports
- GoogleMapPlotter
import gmplot; gmap = gmplot()
from gmplot import GoogleMapPlotter
- gmplot
from gmplot import gmplot
import gmplot
Quickstart
import gmplot
import os
# Replace with your actual coordinates and zoom level
latitude, longitude, zoom = 37.4239163, -122.0946215, 16
# Initialize GoogleMapPlotter with center latitude, longitude, and zoom
# Add your Google Maps API key using os.environ.get('GOOGLE_API_KEY', '')
# to avoid the 'For Development Purposes Only' watermark
gmap = gmplot.GoogleMapPlotter(latitude, longitude, zoom, apikey=os.environ.get('GOOGLE_API_KEY', ''))
# Add a marker
gmap.marker(latitude, longitude, 'cornflowerblue', size=40, title="My Location")
# Add a scatter plot (e.g., nearby points)
more_lats = [37.425, 37.420]
more_lngs = [-122.090, -122.100]
gmap.scatter(more_lats, more_lngs, '#FF0000', size=20, marker=False)
# Draw the map to an HTML file
output_html_file = 'mymap.html'
gmap.draw(output_html_file)
print(f"Map saved to {output_html_file}")