Skip to content

Commit

Permalink
Add 'geodatasets' as a dependency for docs and update the choropleth …
Browse files Browse the repository at this point in the history
…example (#3719)
  • Loading branch information
seisman authored Dec 26, 2024
1 parent 48aeafa commit 41702d7
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 19 deletions.
1 change: 1 addition & 0 deletions .github/workflows/ci_docs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ jobs:
make
pip
python-build
geodatasets
myst-nb
panel
sphinx>=6.2
Expand Down
1 change: 1 addition & 0 deletions ci/requirements/docs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ dependencies:
- pip
- python-build
# Dev dependencies (building documentation)
- geodatasets
- myst-nb
- panel
- sphinx>=6.2
Expand Down
1 change: 1 addition & 0 deletions environment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ dependencies:
- pytest-doctestplus
- pytest-mpl
# Dev dependencies (building documentation)
- geodatasets
- myst-nb
- panel
- sphinx>=6.2
Expand Down
39 changes: 20 additions & 19 deletions examples/gallery/maps/choropleth_map.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,27 @@
Choropleth map
==============
The :meth:`pygmt.Figure.plot` method allows us to plot geographical data such
as polygons which are stored in a :class:`geopandas.GeoDataFrame` object. Use
:func:`geopandas.read_file` to load data from any supported OGR format such as
a shapefile (.shp), GeoJSON (.geojson), geopackage (.gpkg), etc. You can also
use a full URL pointing to your desired data source. Then, pass the
:class:`geopandas.GeoDataFrame` as an argument to the ``data`` parameter of
:meth:`pygmt.Figure.plot`, and style the geometry using the ``pen`` parameter.
To fill the polygons based on a corresponding column you need to set
``fill="+z"`` as well as select the appropriate column using the ``aspatial``
parameter as shown in the example below.
The :meth:`pygmt.Figure.plot` method allows us to plot geographical data such as
polygons which are stored in a :class:`geopandas.GeoDataFrame` object. Use
:func:`geopandas.read_file` to load data from any supported OGR format such as a
shapefile (.shp), GeoJSON (.geojson), geopackage (.gpkg), etc. You can also use a full
URL pointing to your desired data source. Then, pass the class:`geopandas.GeoDataFrame`
as an argument to the ``data`` parameter of :meth:`pygmt.Figure.plot`, and style the
geometry using the ``pen`` parameter. To fill the polygons based on a corresponding
column you need to set ``fill="+z"`` as well as select the appropriate column using the
``aspatial`` parameter as shown in the example below.
"""

# %%
import geodatasets
import geopandas as gpd
import pygmt

# Read polygon data using geopandas
gdf = gpd.read_file("https://geodacenter.github.io/data-and-lab/data/airbnb.zip")
# Read the example dataset provided by geodatasets.
gdf = gpd.read_file(geodatasets.get_path("geoda airbnb"))
print(gdf)

# %%
fig = pygmt.Figure()

fig.basemap(
Expand All @@ -29,20 +31,19 @@
frame="+tPopulation of Chicago",
)

# The dataset contains different attributes, here we select
# the "population" column to plot.
# The dataset contains different attributes, here we select the "population" column to
# plot.

# First, we define the colormap to fill the polygons based on
# the "population" column.
# First, we define the colormap to fill the polygons based on the "population" column.
pygmt.makecpt(
cmap="acton",
series=[gdf["population"].min(), gdf["population"].max(), 10],
continuous=True,
reverse=True,
)

# Next, we plot the polygons and fill them using the defined colormap.
# The target column is defined by the aspatial parameter.
# Next, we plot the polygons and fill them using the defined colormap. The target column
# is defined by the aspatial parameter.
fig.plot(
data=gdf,
pen="0.3p,gray10",
Expand All @@ -51,7 +52,7 @@
aspatial="Z=population",
)

# Add colorbar legend
# Add colorbar legend.
fig.colorbar(frame="x+lPopulation", position="jML+o-0.5c+w3.5c/0.2c")

fig.show()

0 comments on commit 41702d7

Please sign in to comment.