From 52b3534e020e97c00eebf287122afbdd7c1bba00 Mon Sep 17 00:00:00 2001 From: Michael Grund <23025878+michaelgrund@users.noreply.github.com> Date: Sun, 31 Oct 2021 15:00:55 +0100 Subject: [PATCH 01/11] Add gallery example to showcase blockmean --- examples/gallery/histograms/blockm | 48 ++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 examples/gallery/histograms/blockm diff --git a/examples/gallery/histograms/blockm b/examples/gallery/histograms/blockm new file mode 100644 index 00000000000..331e5b4bcb5 --- /dev/null +++ b/examples/gallery/histograms/blockm @@ -0,0 +1,48 @@ +""" +Blockmean +--------- +The :meth:`pygmt.blockmean` method allows to calculate block averages and +to report the number of points inside each block. +""" + +import pygmt + + +# load sample data +data = pygmt.datasets.load_japan_quakes() +# select only needed columns +data = data[['longitude','latitude', "depth_km"]] + +# Set the region for the plot +region = [130, 152.5, 32.5, 52.5] +# Define spacing in x and y direction +spacing = "150m" + +fig = pygmt.Figure() + +# ---------------------------------------------------- +# Calculate mean depth in km from all events within 150x 150 bins using blockmean +df = pygmt.blockmean(data, region = region, spacing = spacing) +# convert to grid +grd = pygmt.xyz2grd(df, region = region, spacing = spacing) + +fig.grdimage(grd, region = region, frame = ["af", '+t"Mean earthquake depth inside each block"'], cmap ="batlow") +# plot slightly transparent landmasses on top +fig.coast(land="darkgray", transparency="40") +# plot original data points +fig.plot(x=data.longitude, y=data.latitude, style="c0.3c", color="white", pen="1p,black") +fig.colorbar(frame=["x+lkm"]) + +fig.shift_origin(xshift="w+5c") + +# ---------------------------------------------------- +# Calculate number of total locations within 150x 150 bins using blockmean +df = pygmt.blockmean(data, region = region, spacing = spacing, S = "n") +grd = pygmt.xyz2grd(df, region = region, spacing = spacing) + +fig.grdimage(grd, region = region, frame = ["af", '+t"Number of points inside each block"'], cmap ="batlow") +fig.coast(land="darkgray", transparency="40") +fig.plot(x=data.longitude, y=data.latitude, style="c0.3c", color="white", pen="1p,black") +fig.colorbar(frame=["x+lcount"]) + +fig.show() From 50977354529db7499585f918a668f47ea34174db Mon Sep 17 00:00:00 2001 From: Michael Grund Date: Mon, 1 Nov 2021 11:23:26 +0100 Subject: [PATCH 02/11] add file ending --- .../gallery/histograms/{blockm => blockm.py} | 39 ++++++++++++------- 1 file changed, 26 insertions(+), 13 deletions(-) rename examples/gallery/histograms/{blockm => blockm.py} (54%) diff --git a/examples/gallery/histograms/blockm b/examples/gallery/histograms/blockm.py similarity index 54% rename from examples/gallery/histograms/blockm rename to examples/gallery/histograms/blockm.py index 331e5b4bcb5..d2c76f94fda 100644 --- a/examples/gallery/histograms/blockm +++ b/examples/gallery/histograms/blockm.py @@ -7,11 +7,10 @@ import pygmt - # load sample data -data = pygmt.datasets.load_japan_quakes() +data = pygmt.datasets.load_japan_quakes() # select only needed columns -data = data[['longitude','latitude', "depth_km"]] +data = data[["longitude", "latitude", "depth_km"]] # Set the region for the plot region = [130, 152.5, 32.5, 52.5] @@ -22,27 +21,41 @@ # ---------------------------------------------------- # Calculate mean depth in km from all events within 150x 150 bins using blockmean -df = pygmt.blockmean(data, region = region, spacing = spacing) +df = pygmt.blockmean(data, region=region, spacing=spacing) # convert to grid -grd = pygmt.xyz2grd(df, region = region, spacing = spacing) - -fig.grdimage(grd, region = region, frame = ["af", '+t"Mean earthquake depth inside each block"'], cmap ="batlow") +grd = pygmt.xyz2grd(df, region=region, spacing=spacing) + +fig.grdimage( + grd, + region=region, + frame=["af", '+t"Mean earthquake depth inside each block"'], + cmap="batlow", +) # plot slightly transparent landmasses on top fig.coast(land="darkgray", transparency="40") # plot original data points -fig.plot(x=data.longitude, y=data.latitude, style="c0.3c", color="white", pen="1p,black") +fig.plot( + x=data.longitude, y=data.latitude, style="c0.3c", color="white", pen="1p,black" +) fig.colorbar(frame=["x+lkm"]) fig.shift_origin(xshift="w+5c") # ---------------------------------------------------- # Calculate number of total locations within 150x 150 bins using blockmean -df = pygmt.blockmean(data, region = region, spacing = spacing, S = "n") -grd = pygmt.xyz2grd(df, region = region, spacing = spacing) - -fig.grdimage(grd, region = region, frame = ["af", '+t"Number of points inside each block"'], cmap ="batlow") +df = pygmt.blockmean(data, region=region, spacing=spacing, S="n") +grd = pygmt.xyz2grd(df, region=region, spacing=spacing) + +fig.grdimage( + grd, + region=region, + frame=["af", '+t"Number of points inside each block"'], + cmap="batlow", +) fig.coast(land="darkgray", transparency="40") -fig.plot(x=data.longitude, y=data.latitude, style="c0.3c", color="white", pen="1p,black") +fig.plot( + x=data.longitude, y=data.latitude, style="c0.3c", color="white", pen="1p,black" +) fig.colorbar(frame=["x+lcount"]) fig.show() From 700561ea52c053bf1f595d137eb26a8ceb5fe618 Mon Sep 17 00:00:00 2001 From: Michael Grund Date: Mon, 1 Nov 2021 11:25:42 +0100 Subject: [PATCH 03/11] formatting --- examples/gallery/histograms/blockm.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/examples/gallery/histograms/blockm.py b/examples/gallery/histograms/blockm.py index d2c76f94fda..0a2fb743be9 100644 --- a/examples/gallery/histograms/blockm.py +++ b/examples/gallery/histograms/blockm.py @@ -20,7 +20,8 @@ fig = pygmt.Figure() # ---------------------------------------------------- -# Calculate mean depth in km from all events within 150x 150 bins using blockmean +# Calculate mean depth in km from all events within 150x 150 +# bins using blockmean df = pygmt.blockmean(data, region=region, spacing=spacing) # convert to grid grd = pygmt.xyz2grd(df, region=region, spacing=spacing) From 62f941bb8a59913bca487956e97545926546cace Mon Sep 17 00:00:00 2001 From: Michael Grund <23025878+michaelgrund@users.noreply.github.com> Date: Fri, 5 Nov 2021 18:34:03 +0100 Subject: [PATCH 04/11] Update examples/gallery/histograms/blockm.py Co-authored-by: Will Schlitzer --- examples/gallery/histograms/blockm.py | 1 - 1 file changed, 1 deletion(-) diff --git a/examples/gallery/histograms/blockm.py b/examples/gallery/histograms/blockm.py index 0a2fb743be9..28e208b41b9 100644 --- a/examples/gallery/histograms/blockm.py +++ b/examples/gallery/histograms/blockm.py @@ -19,7 +19,6 @@ fig = pygmt.Figure() -# ---------------------------------------------------- # Calculate mean depth in km from all events within 150x 150 # bins using blockmean df = pygmt.blockmean(data, region=region, spacing=spacing) From 605bba5d2f0b7b4b4ca1ceb913aa002832479a9f Mon Sep 17 00:00:00 2001 From: Michael Grund Date: Mon, 8 Nov 2021 18:32:10 +0100 Subject: [PATCH 05/11] use summary alias --- examples/gallery/histograms/blockm.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/examples/gallery/histograms/blockm.py b/examples/gallery/histograms/blockm.py index 28e208b41b9..0f2daaef131 100644 --- a/examples/gallery/histograms/blockm.py +++ b/examples/gallery/histograms/blockm.py @@ -14,12 +14,12 @@ # Set the region for the plot region = [130, 152.5, 32.5, 52.5] -# Define spacing in x and y direction +# Define spacing in x and y direction (150 by 150 minute blocks) spacing = "150m" fig = pygmt.Figure() -# Calculate mean depth in km from all events within 150x 150 +# Calculate mean depth in km from all events within 150x150 minute # bins using blockmean df = pygmt.blockmean(data, region=region, spacing=spacing) # convert to grid @@ -42,8 +42,9 @@ fig.shift_origin(xshift="w+5c") # ---------------------------------------------------- -# Calculate number of total locations within 150x 150 bins using blockmean -df = pygmt.blockmean(data, region=region, spacing=spacing, S="n") +# Calculate number of total locations within 150x150 minute bins via +# blockmean's summary parameter +df = pygmt.blockmean(data, region=region, spacing=spacing, summary="n") grd = pygmt.xyz2grd(df, region=region, spacing=spacing) fig.grdimage( From e771d64cb307b459589c8b7659876c01c1346102 Mon Sep 17 00:00:00 2001 From: Michael Grund Date: Tue, 9 Nov 2021 19:07:50 +0100 Subject: [PATCH 06/11] update docstring --- examples/gallery/histograms/blockm.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/examples/gallery/histograms/blockm.py b/examples/gallery/histograms/blockm.py index 0f2daaef131..9ef7d2eae79 100644 --- a/examples/gallery/histograms/blockm.py +++ b/examples/gallery/histograms/blockm.py @@ -1,15 +1,17 @@ """ Blockmean --------- -The :meth:`pygmt.blockmean` method allows to calculate block averages and -to report the number of points inside each block. +The :meth:`pygmt.blockmean` method allows to calculate different quantities +inside blocks/bins whose dimensions are defined via the ``spacing`` parameter. +The following example shows how to calculate averages of given values inside +each block as well as how to report the number of points inside each bin. """ import pygmt -# load sample data +# Load sample data data = pygmt.datasets.load_japan_quakes() -# select only needed columns +# Select only needed columns data = data[["longitude", "latitude", "depth_km"]] # Set the region for the plot @@ -41,7 +43,6 @@ fig.shift_origin(xshift="w+5c") -# ---------------------------------------------------- # Calculate number of total locations within 150x150 minute bins via # blockmean's summary parameter df = pygmt.blockmean(data, region=region, spacing=spacing, summary="n") From a500bd1d4a199858572a1560a19990e526804467 Mon Sep 17 00:00:00 2001 From: Michael Grund <23025878+michaelgrund@users.noreply.github.com> Date: Thu, 18 Nov 2021 10:14:45 +0100 Subject: [PATCH 07/11] Apply suggestions from code review Co-authored-by: Will Schlitzer --- examples/gallery/histograms/blockm.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/examples/gallery/histograms/blockm.py b/examples/gallery/histograms/blockm.py index 9ef7d2eae79..fddb67f1432 100644 --- a/examples/gallery/histograms/blockm.py +++ b/examples/gallery/histograms/blockm.py @@ -1,9 +1,9 @@ """ Blockmean --------- -The :meth:`pygmt.blockmean` method allows to calculate different quantities +The :meth:`pygmt.blockmean` method calculates different quantities inside blocks/bins whose dimensions are defined via the ``spacing`` parameter. -The following example shows how to calculate averages of given values inside +The following examples show how to calculate averages of given values inside each block as well as how to report the number of points inside each bin. """ @@ -23,12 +23,12 @@ # Calculate mean depth in km from all events within 150x150 minute # bins using blockmean -df = pygmt.blockmean(data, region=region, spacing=spacing) +df = pygmt.blockmean(data=data, region=region, spacing=spacing) # convert to grid -grd = pygmt.xyz2grd(df, region=region, spacing=spacing) +grd = pygmt.xyz2grd(data=df, region=region, spacing=spacing) fig.grdimage( - grd, + grid=grd, region=region, frame=["af", '+t"Mean earthquake depth inside each block"'], cmap="batlow", @@ -45,11 +45,11 @@ # Calculate number of total locations within 150x150 minute bins via # blockmean's summary parameter -df = pygmt.blockmean(data, region=region, spacing=spacing, summary="n") -grd = pygmt.xyz2grd(df, region=region, spacing=spacing) +df = pygmt.blockmean(data=data, region=region, spacing=spacing, summary="n") +grd = pygmt.xyz2grd(data=df, region=region, spacing=spacing) fig.grdimage( - grd, + grid=grd, region=region, frame=["af", '+t"Number of points inside each block"'], cmap="batlow", From 53e5c62c3813afa3ff861abc7bc031dea4e08365 Mon Sep 17 00:00:00 2001 From: Michael Grund <23025878+michaelgrund@users.noreply.github.com> Date: Thu, 18 Nov 2021 12:06:14 +0100 Subject: [PATCH 08/11] Apply suggestions from code review Co-authored-by: Will Schlitzer --- examples/gallery/histograms/blockm.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/examples/gallery/histograms/blockm.py b/examples/gallery/histograms/blockm.py index fddb67f1432..e4e482a5c93 100644 --- a/examples/gallery/histograms/blockm.py +++ b/examples/gallery/histograms/blockm.py @@ -3,8 +3,8 @@ --------- The :meth:`pygmt.blockmean` method calculates different quantities inside blocks/bins whose dimensions are defined via the ``spacing`` parameter. -The following examples show how to calculate averages of given values inside -each block as well as how to report the number of points inside each bin. +The following examples show how to calculate the averages of the given values inside +each block and how to report the number of points inside each bin. """ import pygmt From fca3f1231998ccca82ec8c89a7e5abdae791e0a5 Mon Sep 17 00:00:00 2001 From: Michael Grund <23025878+michaelgrund@users.noreply.github.com> Date: Fri, 19 Nov 2021 08:06:46 +0100 Subject: [PATCH 09/11] Apply suggestions from code review Co-authored-by: Will Schlitzer --- examples/gallery/histograms/blockm.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/gallery/histograms/blockm.py b/examples/gallery/histograms/blockm.py index e4e482a5c93..ad9e71ba874 100644 --- a/examples/gallery/histograms/blockm.py +++ b/examples/gallery/histograms/blockm.py @@ -4,7 +4,7 @@ The :meth:`pygmt.blockmean` method calculates different quantities inside blocks/bins whose dimensions are defined via the ``spacing`` parameter. The following examples show how to calculate the averages of the given values inside -each block and how to report the number of points inside each bin. +each bin and how to report the number of points inside each bin. """ import pygmt From 471d4ef96851b388026093e19cecc5df1e8900ef Mon Sep 17 00:00:00 2001 From: Michael Grund Date: Fri, 19 Nov 2021 08:09:56 +0100 Subject: [PATCH 10/11] formatting --- examples/gallery/histograms/blockm.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/examples/gallery/histograms/blockm.py b/examples/gallery/histograms/blockm.py index ad9e71ba874..6b741c9c843 100644 --- a/examples/gallery/histograms/blockm.py +++ b/examples/gallery/histograms/blockm.py @@ -3,8 +3,8 @@ --------- The :meth:`pygmt.blockmean` method calculates different quantities inside blocks/bins whose dimensions are defined via the ``spacing`` parameter. -The following examples show how to calculate the averages of the given values inside -each bin and how to report the number of points inside each bin. +The following examples show how to calculate the averages of the given values +inside each bin and how to report the number of points inside each bin. """ import pygmt From ce03917c58681f57543bbdea04b61b521ae9014b Mon Sep 17 00:00:00 2001 From: Michael Grund <23025878+michaelgrund@users.noreply.github.com> Date: Sat, 20 Nov 2021 13:06:10 +0100 Subject: [PATCH 11/11] Apply suggestions from code review Co-authored-by: Dongdong Tian --- examples/gallery/histograms/blockm.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/examples/gallery/histograms/blockm.py b/examples/gallery/histograms/blockm.py index 6b741c9c843..87c35347392 100644 --- a/examples/gallery/histograms/blockm.py +++ b/examples/gallery/histograms/blockm.py @@ -34,7 +34,7 @@ cmap="batlow", ) # plot slightly transparent landmasses on top -fig.coast(land="darkgray", transparency="40") +fig.coast(land="darkgray", transparency=40) # plot original data points fig.plot( x=data.longitude, y=data.latitude, style="c0.3c", color="white", pen="1p,black" @@ -54,7 +54,7 @@ frame=["af", '+t"Number of points inside each block"'], cmap="batlow", ) -fig.coast(land="darkgray", transparency="40") +fig.coast(land="darkgray", transparency=40) fig.plot( x=data.longitude, y=data.latitude, style="c0.3c", color="white", pen="1p,black" )