Integration with geopandas to plot shapely geometries
See original GitHub issueDescription of the desired feature
Geopandas is somewhat of a defacto geographic data structure in Python nowadays, and is used to hold vector shapes like Polygons, Lines and Points. What might be useful is if we can have PyGMT plot geopandas Data Structures (e.g. geopandas.GeoDataFrames) directly. Something like so:
import geopandas as gpd
import pygmt
cities: gpd.GeoDataFrame = gpd.read_file(gpd.datasets.get_path("naturalearth_cities"))
cities.head()
# name geometry
# 0 Vatican City POINT (12.45339 41.90328)
# 1 San Marino POINT (12.44177 43.93610)
# 2 Vaduz POINT (9.51667 47.13372)
# 3 Luxembourg POINT (6.13000 49.61166)
# 4 Palikir POINT (158.14997 6.91664)
fig = pygmt.Figure()
fig.plot(data=cities, style="s0.2c", color="black")
fig.show()
One way would be to change plot to accept geopandas type objects, look for the ‘geometry’ column, and plot the ‘Shapely’ geometries.
Currently, users need to go through a convoluted process such as with the following (adapted from https://forum.generic-mapping-tools.org/t/shapefile-to-gmt-python/834/21, also xref https://github.com/GenericMappingTools/foss4g2019oceania/pull/7):
fig = pygmt.Figure()
fig.coast(region="g", land="gray", water="lightblue")
points = [point for point in cities.geometry[:10]] # plot 10 cities only
for point in points:
x, y = point.coords.xy
fig.plot(x=x, y=y, style="s0.2c", color="black")
fig.show()
produces:

Using data=geodataframe seems like the easiest, but we could consider alternative ways of implementing the geopandas integration. There might need to be separate code pathways for handling Points, Lines, and Polygons.
👋 Would be good to hear from the geo-community: What is your preferred way of putting data into a plot function?
- The plot-function-first-then-data way, i.e.
fig.plot(data=geodata) - The data-first-then-plot accessor way
geodata.gmt.plot(style=...)
Are you willing to help implement and maintain this feature? Yes, but discuss first 😄
Issue Analytics
- State:
- Created 3 years ago
- Comments:12 (6 by maintainers)

Top Related StackOverflow Question
FYI, I’m working on #961 which will lay the groundwork for
geopandasintegration (by providing a single I/O entrypoint into GMT virtualfiles). Still need to work on the implementation details, but hopefully 🤞 this will mean that any PyGMT function which accepts a table-like input will be able to take ingeopandas.GeoDataFrametoo (i.e. not justfig.plot), but will need more testing on points/lines/polygons to see if this does actually work!Hi! This would be super-useful, and your “hack” already works great for plotting geoDataFrames of Polygons imported from
.shpfiles on top of.grd“images/maps”.LMK if you need help with testing and such!