Skip to content

Commit 10c434f

Browse files
michaelgrundyvonnefroehlichseisman
authored
Add gallery example showing usage of polygon objects from a geopandas.GeoDataFrame (choropleth map) (#2796)
Co-authored-by: Yvonne Fröhlich <[email protected]> Co-authored-by: Dongdong Tian <[email protected]>
1 parent 4be7775 commit 10c434f

File tree

1 file changed

+56
-0
lines changed

1 file changed

+56
-0
lines changed
+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
"""
2+
Choropleth Map
3+
==============
4+
5+
The :meth:`pygmt.Figure.plot` method allows us to plot geographical data such
6+
as polygons which are stored in a :class:`geopandas.GeoDataFrame` object. Use
7+
:func:`geopandas.read_file` to load data from any supported OGR format such as
8+
a shapefile (.shp), GeoJSON (.geojson), geopackage (.gpkg), etc. You can also
9+
use a full URL pointing to your desired data source. Then, pass the
10+
:class:`geopandas.GeoDataFrame` as an argument to the ``data`` parameter of
11+
:meth:`pygmt.Figure.plot`, and style the geometry using the ``pen`` parameter.
12+
To fill the polygons based on a corresponding column you need to set
13+
``fill="+z"`` as well as select the appropriate column using the ``aspatial``
14+
parameter as shown in the example below.
15+
"""
16+
17+
import geopandas as gpd
18+
import pygmt
19+
20+
# Read polygon data using geopandas
21+
gdf = gpd.read_file("https://geodacenter.github.io/data-and-lab/data/airbnb.zip")
22+
23+
fig = pygmt.Figure()
24+
25+
fig.basemap(
26+
region=gdf.total_bounds[[0, 2, 1, 3]],
27+
projection="M6c",
28+
frame="+tPopulation of Chicago",
29+
)
30+
31+
# The dataset contains different attributes, here we select
32+
# the "population" column to plot.
33+
34+
# First, we define the colormap to fill the polygons based on
35+
# the "population" column.
36+
pygmt.makecpt(
37+
cmap="acton",
38+
series=[gdf["population"].min(), gdf["population"].max(), 10],
39+
continuous=True,
40+
reverse=True,
41+
)
42+
43+
# Next, we plot the polygons and fill them using the defined colormap.
44+
# The target column is defined by the aspatial parameter.
45+
fig.plot(
46+
data=gdf,
47+
pen="0.3p,gray10",
48+
fill="+z",
49+
cmap=True,
50+
aspatial="Z=population",
51+
)
52+
53+
# Add colorbar legend
54+
fig.colorbar(frame="x+lPopulation", position="jML+o-0.5c+w3.5c/0.2c")
55+
56+
fig.show()

0 commit comments

Comments
 (0)