From ddda358cea440ce97df58b8bf54b1b4a15660d45 Mon Sep 17 00:00:00 2001 From: Michael Grund <23025878+michaelgrund@users.noreply.github.com> Date: Fri, 3 Nov 2023 10:43:47 +0100 Subject: [PATCH 01/15] Create polygons.py --- examples/gallery/lines/polygons.py | 67 ++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 examples/gallery/lines/polygons.py diff --git a/examples/gallery/lines/polygons.py b/examples/gallery/lines/polygons.py new file mode 100644 index 00000000000..c9310544c90 --- /dev/null +++ b/examples/gallery/lines/polygons.py @@ -0,0 +1,67 @@ +""" +Filled polygons +=============== + +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 in +: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 numpy as np +import pygmt + +# Read polygon data using geopandas +gdf = gpd.read_file("https://geodacenter.github.io/data-and-lab//data/airbnb.zip") +# Automatically get min/max coordinates of polygon set +bounds = gdf.total_bounds +# Define an edge in degrees to add in each direction to the min/max coordinates +edge = 0.02 + +fig = pygmt.Figure() + +fig.coast( + region=[bounds[0]-edge, bounds[2]+edge, bounds[1]-edge, bounds[3]+edge], + projection="M10c", + frame=["af", "+tPopulation of Chicago"], + water="lightblue", + land="gray70", + shorelines="1/1p,gray70", +) + +# The dataset contains different parameters, here we select +# the "population" column to plot. + +# First, we define the colormap to fill the polygons based on +# only the "population" column. +pygmt.makecpt( + cmap="acton", + series=[np.min(gdf["population"]), np.max(gdf["population"]), 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[["population","geometry"]], + pen="0.3p,gray10", + close=True, + fill="+z", + cmap=True, + aspatial="Z=population", +) + + +# Add colorbar legend +fig.colorbar(frame="x+lPopulation") + +fig.show() From cf53b9bae8edbf3de045ea2a40114a79811e61c6 Mon Sep 17 00:00:00 2001 From: actions-bot <58130806+actions-bot@users.noreply.github.com> Date: Fri, 3 Nov 2023 09:54:19 +0000 Subject: [PATCH 02/15] [format-command] fixes --- examples/gallery/lines/polygons.py | 32 +++++++++++++++--------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/examples/gallery/lines/polygons.py b/examples/gallery/lines/polygons.py index c9310544c90..cf28d289e85 100644 --- a/examples/gallery/lines/polygons.py +++ b/examples/gallery/lines/polygons.py @@ -5,12 +5,12 @@ 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 +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 in :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`` +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. """ @@ -22,24 +22,24 @@ gdf = gpd.read_file("https://geodacenter.github.io/data-and-lab//data/airbnb.zip") # Automatically get min/max coordinates of polygon set bounds = gdf.total_bounds -# Define an edge in degrees to add in each direction to the min/max coordinates +# Define an edge in degrees to add in each direction to the min/max coordinates edge = 0.02 fig = pygmt.Figure() fig.coast( - region=[bounds[0]-edge, bounds[2]+edge, bounds[1]-edge, bounds[3]+edge], - projection="M10c", + region=[bounds[0] - edge, bounds[2] + edge, bounds[1] - edge, bounds[3] + edge], + projection="M10c", frame=["af", "+tPopulation of Chicago"], water="lightblue", land="gray70", shorelines="1/1p,gray70", ) -# The dataset contains different parameters, here we select -# the "population" column to plot. +# The dataset contains different parameters, here we select +# the "population" column to plot. -# First, we define the colormap to fill the polygons based on +# First, we define the colormap to fill the polygons based on # only the "population" column. pygmt.makecpt( cmap="acton", @@ -48,14 +48,14 @@ 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[["population","geometry"]], - pen="0.3p,gray10", - close=True, - fill="+z", + data=gdf[["population", "geometry"]], + pen="0.3p,gray10", + close=True, + fill="+z", cmap=True, aspatial="Z=population", ) From 999545a98fc040ec34a119d78e16b0dc20696441 Mon Sep 17 00:00:00 2001 From: Michael Grund <23025878+michaelgrund@users.noreply.github.com> Date: Fri, 3 Nov 2023 15:01:34 +0100 Subject: [PATCH 03/15] Apply suggestions from code review 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/lines/polygons.py | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/examples/gallery/lines/polygons.py b/examples/gallery/lines/polygons.py index cf28d289e85..a6448f4b8f4 100644 --- a/examples/gallery/lines/polygons.py +++ b/examples/gallery/lines/polygons.py @@ -7,10 +7,10 @@ :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 in +: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`` +``fill="+z"`` as well as select the appropriate column using the ``aspatial`` parameter as shown in the example below. """ @@ -27,23 +27,20 @@ fig = pygmt.Figure() -fig.coast( +fig.basemap( region=[bounds[0] - edge, bounds[2] + edge, bounds[1] - edge, bounds[3] + edge], projection="M10c", - frame=["af", "+tPopulation of Chicago"], - water="lightblue", - land="gray70", - shorelines="1/1p,gray70", + frame="+tPopulation of Chicago", ) # The dataset contains different parameters, here we select # the "population" column to plot. # First, we define the colormap to fill the polygons based on -# only the "population" column. +# the "population" column. pygmt.makecpt( cmap="acton", - series=[np.min(gdf["population"]), np.max(gdf["population"]), 10], + series=[gdf["population"].min(), gdf["population"].max(), 10], continuous=True, reverse=True, ) From 4bd9f7f68ac52408e13ec2b542cb730e574b4dbe Mon Sep 17 00:00:00 2001 From: Michael Grund <23025878+michaelgrund@users.noreply.github.com> Date: Fri, 3 Nov 2023 15:03:37 +0100 Subject: [PATCH 04/15] remove np import --- examples/gallery/lines/polygons.py | 1 - 1 file changed, 1 deletion(-) diff --git a/examples/gallery/lines/polygons.py b/examples/gallery/lines/polygons.py index a6448f4b8f4..130be4bca1b 100644 --- a/examples/gallery/lines/polygons.py +++ b/examples/gallery/lines/polygons.py @@ -15,7 +15,6 @@ """ import geopandas as gpd -import numpy as np import pygmt # Read polygon data using geopandas From 1b0db1ee9694972ecc57871ce82d4d2f6ae691da Mon Sep 17 00:00:00 2001 From: Michael Grund <23025878+michaelgrund@users.noreply.github.com> Date: Fri, 3 Nov 2023 15:34:57 +0100 Subject: [PATCH 05/15] Apply suggestions from code review Co-authored-by: Dongdong Tian --- examples/gallery/lines/polygons.py | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/examples/gallery/lines/polygons.py b/examples/gallery/lines/polygons.py index 130be4bca1b..35b598487c6 100644 --- a/examples/gallery/lines/polygons.py +++ b/examples/gallery/lines/polygons.py @@ -19,15 +19,12 @@ # Read polygon data using geopandas gdf = gpd.read_file("https://geodacenter.github.io/data-and-lab//data/airbnb.zip") -# Automatically get min/max coordinates of polygon set -bounds = gdf.total_bounds -# Define an edge in degrees to add in each direction to the min/max coordinates -edge = 0.02 fig = pygmt.Figure() fig.basemap( - region=[bounds[0] - edge, bounds[2] + edge, bounds[1] - edge, bounds[3] + edge], + # Automatically get min/max coordinates of polygon set + region=gdf.total_bounds[[0, 2, 1, 3]], projection="M10c", frame="+tPopulation of Chicago", ) From 784df8b8bc4de17ed419a7dab94b2e50fce878a5 Mon Sep 17 00:00:00 2001 From: Michael Grund <23025878+michaelgrund@users.noreply.github.com> Date: Fri, 3 Nov 2023 16:32:14 +0100 Subject: [PATCH 06/15] Update and rename polygons.py to choropleth_map.py --- examples/gallery/lines/{polygons.py => choropleth_map.py} | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) rename examples/gallery/lines/{polygons.py => choropleth_map.py} (98%) diff --git a/examples/gallery/lines/polygons.py b/examples/gallery/lines/choropleth_map.py similarity index 98% rename from examples/gallery/lines/polygons.py rename to examples/gallery/lines/choropleth_map.py index 35b598487c6..20a3ca19bda 100644 --- a/examples/gallery/lines/polygons.py +++ b/examples/gallery/lines/choropleth_map.py @@ -1,6 +1,6 @@ """ -Filled polygons -=============== +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 From d2259213ca2e99ad00fa5ae8d134a47418e24c96 Mon Sep 17 00:00:00 2001 From: Michael Grund <23025878+michaelgrund@users.noreply.github.com> Date: Mon, 6 Nov 2023 10:52:19 +0100 Subject: [PATCH 07/15] Apply suggestions from code review Co-authored-by: Dongdong Tian --- examples/gallery/lines/choropleth_map.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/examples/gallery/lines/choropleth_map.py b/examples/gallery/lines/choropleth_map.py index 20a3ca19bda..4cd88cecbe9 100644 --- a/examples/gallery/lines/choropleth_map.py +++ b/examples/gallery/lines/choropleth_map.py @@ -18,7 +18,7 @@ import pygmt # Read polygon data using geopandas -gdf = gpd.read_file("https://geodacenter.github.io/data-and-lab//data/airbnb.zip") +gdf = gpd.read_file("https://geodacenter.github.io/data-and-lab/data/airbnb.zip") fig = pygmt.Figure() @@ -29,7 +29,7 @@ frame="+tPopulation of Chicago", ) -# The dataset contains different parameters, here we select +# The dataset contains different attributes, here we select # the "population" column to plot. # First, we define the colormap to fill the polygons based on From 72a342aa278ff676700eac596ea4a88e1760276c Mon Sep 17 00:00:00 2001 From: Michael Grund <23025878+michaelgrund@users.noreply.github.com> Date: Mon, 6 Nov 2023 10:53:02 +0100 Subject: [PATCH 08/15] Rename choropleth_map.py to choropleth_map.py --- examples/gallery/{lines => maps}/choropleth_map.py | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename examples/gallery/{lines => maps}/choropleth_map.py (100%) diff --git a/examples/gallery/lines/choropleth_map.py b/examples/gallery/maps/choropleth_map.py similarity index 100% rename from examples/gallery/lines/choropleth_map.py rename to examples/gallery/maps/choropleth_map.py From bd158c8d14a5e294ec27cb39ae4bfdb6de9faa8c Mon Sep 17 00:00:00 2001 From: Michael Grund <23025878+michaelgrund@users.noreply.github.com> Date: Mon, 6 Nov 2023 10:54:35 +0100 Subject: [PATCH 09/15] Update choropleth_map.py --- examples/gallery/maps/choropleth_map.py | 1 - 1 file changed, 1 deletion(-) diff --git a/examples/gallery/maps/choropleth_map.py b/examples/gallery/maps/choropleth_map.py index 4cd88cecbe9..6154bf8d48a 100644 --- a/examples/gallery/maps/choropleth_map.py +++ b/examples/gallery/maps/choropleth_map.py @@ -23,7 +23,6 @@ fig = pygmt.Figure() fig.basemap( - # Automatically get min/max coordinates of polygon set region=gdf.total_bounds[[0, 2, 1, 3]], projection="M10c", frame="+tPopulation of Chicago", From 4afbe1cca0f257492456dc415b01329f4219a0e4 Mon Sep 17 00:00:00 2001 From: Michael Grund <23025878+michaelgrund@users.noreply.github.com> Date: Mon, 6 Nov 2023 11:03:23 +0100 Subject: [PATCH 10/15] Update choropleth_map.py --- examples/gallery/maps/choropleth_map.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/gallery/maps/choropleth_map.py b/examples/gallery/maps/choropleth_map.py index 6154bf8d48a..fe70cc0642e 100644 --- a/examples/gallery/maps/choropleth_map.py +++ b/examples/gallery/maps/choropleth_map.py @@ -54,6 +54,6 @@ # Add colorbar legend -fig.colorbar(frame="x+lPopulation") +fig.colorbar(frame="x+lPopulation", position="jML+w6c/0.5c") fig.show() From 6f9ccdd6a17991ec6efdfcb8e00aadd72c825174 Mon Sep 17 00:00:00 2001 From: Michael Grund <23025878+michaelgrund@users.noreply.github.com> Date: Tue, 7 Nov 2023 16:08:58 +0100 Subject: [PATCH 11/15] Apply suggestions from code review Co-authored-by: Dongdong Tian --- examples/gallery/maps/choropleth_map.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/gallery/maps/choropleth_map.py b/examples/gallery/maps/choropleth_map.py index fe70cc0642e..2bf6d96bd51 100644 --- a/examples/gallery/maps/choropleth_map.py +++ b/examples/gallery/maps/choropleth_map.py @@ -44,7 +44,7 @@ # colormap. The target column is defined by the aspatial # parameter. fig.plot( - data=gdf[["population", "geometry"]], + data=gdf, pen="0.3p,gray10", close=True, fill="+z", From 9654a2034068c63e466f4bcc88aeb76bf02f10b9 Mon Sep 17 00:00:00 2001 From: Michael Grund <23025878+michaelgrund@users.noreply.github.com> Date: Tue, 7 Nov 2023 16:09:15 +0100 Subject: [PATCH 12/15] Update choropleth_map.py --- examples/gallery/maps/choropleth_map.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/gallery/maps/choropleth_map.py b/examples/gallery/maps/choropleth_map.py index 2bf6d96bd51..ad84ae5ee3c 100644 --- a/examples/gallery/maps/choropleth_map.py +++ b/examples/gallery/maps/choropleth_map.py @@ -24,7 +24,7 @@ fig.basemap( region=gdf.total_bounds[[0, 2, 1, 3]], - projection="M10c", + projection="M6c", frame="+tPopulation of Chicago", ) From 961e352fbbb39a27028e87f3bf58d73a1dbf816a Mon Sep 17 00:00:00 2001 From: Michael Grund <23025878+michaelgrund@users.noreply.github.com> Date: Wed, 8 Nov 2023 12:56:30 +0100 Subject: [PATCH 13/15] Update choropleth_map.py --- examples/gallery/maps/choropleth_map.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/gallery/maps/choropleth_map.py b/examples/gallery/maps/choropleth_map.py index ad84ae5ee3c..a599540046d 100644 --- a/examples/gallery/maps/choropleth_map.py +++ b/examples/gallery/maps/choropleth_map.py @@ -54,6 +54,6 @@ # Add colorbar legend -fig.colorbar(frame="x+lPopulation", position="jML+w6c/0.5c") +fig.colorbar(frame="x+lPopulation", position="jML+o-0.5c+w3.5c/0.2c") fig.show() From d81aefb88057dd242aa8012264ddadaef9a55e44 Mon Sep 17 00:00:00 2001 From: Michael Grund <23025878+michaelgrund@users.noreply.github.com> Date: Wed, 8 Nov 2023 12:58:59 +0100 Subject: [PATCH 14/15] remove close parameter --- examples/gallery/maps/choropleth_map.py | 1 - 1 file changed, 1 deletion(-) diff --git a/examples/gallery/maps/choropleth_map.py b/examples/gallery/maps/choropleth_map.py index a599540046d..12b53e31a6a 100644 --- a/examples/gallery/maps/choropleth_map.py +++ b/examples/gallery/maps/choropleth_map.py @@ -46,7 +46,6 @@ fig.plot( data=gdf, pen="0.3p,gray10", - close=True, fill="+z", cmap=True, aspatial="Z=population", From c53869f8fef09e12c6e894c4b43b1760aef0ad8b Mon Sep 17 00:00:00 2001 From: Michael Grund <23025878+michaelgrund@users.noreply.github.com> Date: Thu, 9 Nov 2023 10:51:17 +0100 Subject: [PATCH 15/15] Apply suggestions from code review 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> --- examples/gallery/maps/choropleth_map.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/examples/gallery/maps/choropleth_map.py b/examples/gallery/maps/choropleth_map.py index 12b53e31a6a..62f9fbdea9b 100644 --- a/examples/gallery/maps/choropleth_map.py +++ b/examples/gallery/maps/choropleth_map.py @@ -40,9 +40,8 @@ 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", @@ -51,7 +50,6 @@ aspatial="Z=population", ) - # Add colorbar legend fig.colorbar(frame="x+lPopulation", position="jML+o-0.5c+w3.5c/0.2c")