From 10c434ffa7c67dc1746b03c77b4df3b91346d0f5 Mon Sep 17 00:00:00 2001 From: Michael Grund <23025878+michaelgrund@users.noreply.github.com> Date: Fri, 10 Nov 2023 04:19:29 +0100 Subject: [PATCH] Add gallery example showing usage of polygon objects from a geopandas.GeoDataFrame (choropleth map) (#2796) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Yvonne Fröhlich <94163266+yvonnefroehlich@users.noreply.github.com> Co-authored-by: Dongdong Tian --- examples/gallery/maps/choropleth_map.py | 56 +++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 examples/gallery/maps/choropleth_map.py diff --git a/examples/gallery/maps/choropleth_map.py b/examples/gallery/maps/choropleth_map.py new file mode 100644 index 00000000000..62f9fbdea9b --- /dev/null +++ b/examples/gallery/maps/choropleth_map.py @@ -0,0 +1,56 @@ +""" +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. +""" + +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") + +fig = pygmt.Figure() + +fig.basemap( + region=gdf.total_bounds[[0, 2, 1, 3]], + projection="M6c", + frame="+tPopulation of Chicago", +) + +# 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. +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. +fig.plot( + data=gdf, + pen="0.3p,gray10", + fill="+z", + cmap=True, + aspatial="Z=population", +) + +# Add colorbar legend +fig.colorbar(frame="x+lPopulation", position="jML+o-0.5c+w3.5c/0.2c") + +fig.show()