From 7f5c0dc0f764b5b30f9db40bc5edbf9a60677083 Mon Sep 17 00:00:00 2001 From: Aayush7788 Date: Mon, 27 Jul 2026 19:26:02 +0530 Subject: [PATCH 01/11] Add blockmedian and surface gallery example --- .../gallery/images/gridding_with_surface.py | 113 ++++++++++++++++++ 1 file changed, 113 insertions(+) create mode 100644 examples/gallery/images/gridding_with_surface.py diff --git a/examples/gallery/images/gridding_with_surface.py b/examples/gallery/images/gridding_with_surface.py new file mode 100644 index 00000000000..501e03e9183 --- /dev/null +++ b/examples/gallery/images/gridding_with_surface.py @@ -0,0 +1,113 @@ +""" +Gridding scattered data with blockmedian and surface +==================================================== + +The :func:`pygmt.surface` function interpolates irregularly spaced +(x, y, z) observations onto a regular grid. Before interpolation, +:func:`pygmt.blockmedian` reduces redundant observations by returning one +median value for every occupied block. + +""" + +# %% +import pygmt +from pygmt.params import Axis, Frame, Position + +# Load irregular ship-track observations of bathymetry off Baja California. +data = pygmt.datasets.load_sample_data(name="bathymetry") + +region = [245, 255, 20, 30] +spacing = "5m" + +# Calculate one representative median value for each occupied block. +reduced_data = pygmt.blockmedian( + data=data, + region=region, + spacing=spacing, + C=True, +) + +# Interpolate the reduced observations onto a regular grid. +grid = pygmt.surface( + data=reduced_data, + region=region, + spacing=spacing, + lower="d", + upper="d", +) + +fig = pygmt.Figure() + +pygmt.makecpt( + cmap="gmt/geo", + series=[-7500, 0, 500], +) + +pygmt.config( + FONT_TITLE="12p", + MAP_TITLE_OFFSET="4p", +) + +with fig.subplot( + nrows=1, + ncols=2, + figsize=("20c", "9c"), + margins="0.5c", +): + # Block-median observations. + with fig.set_panel(panel=0): + fig.basemap( + region=region, + projection="M?", + frame=Frame( + axes="WSne", + axis=Axis(annot=2, tick=1), + title="Block-median observations", + ), + ) + fig.plot( + x=reduced_data.longitude, + y=reduced_data.latitude, + fill=reduced_data.bathymetry, + style="c0.05c", + cmap=True, + ) + fig.coast( + land="gray", + shorelines="0.5p,black", + ) + + # Interpolated surface grid. + with fig.set_panel(panel=1): + fig.grdimage( + grid=grid, + region=region, + projection="M?", + frame=Frame( + axes="WSne", + axis=Axis(annot=2, tick=1), + title="Interpolated surface", + ), + cmap=True, + ) + fig.coast( + land="gray", + shorelines="0.5p,black", + ) + +# Position the color bar using explicit plot coordinates: +# 10 cm is the horizontal center of the 20 cm wide subplot. +fig.colorbar( + position=Position( + ("10c", "-1.2c"), + cstype="plotcoords", + anchor="TC", + ), + length=12, + width=0.4, + orientation="horizontal", + annot=1000, + label="Bathymetry (m)", +) + +fig.show() From 3ff5eeb46d3e661fe9c377d265b6b4978b812c26 Mon Sep 17 00:00:00 2001 From: Aayush7788 Date: Tue, 28 Jul 2026 09:52:37 +0530 Subject: [PATCH 02/11] Apply review suggestions --- examples/gallery/images/gridding_with_surface.py | 15 +++------------ 1 file changed, 3 insertions(+), 12 deletions(-) diff --git a/examples/gallery/images/gridding_with_surface.py b/examples/gallery/images/gridding_with_surface.py index 501e03e9183..d7f7319bbd7 100644 --- a/examples/gallery/images/gridding_with_surface.py +++ b/examples/gallery/images/gridding_with_surface.py @@ -38,10 +38,7 @@ fig = pygmt.Figure() -pygmt.makecpt( - cmap="gmt/geo", - series=[-7500, 0, 500], -) +pygmt.makecpt(cmap="gmt/geo", series=[-7500, 0, 500]) pygmt.config( FONT_TITLE="12p", @@ -72,10 +69,7 @@ style="c0.05c", cmap=True, ) - fig.coast( - land="gray", - shorelines="0.5p,black", - ) + fig.coast(land="gray", shorelines="0.5p,black") # Interpolated surface grid. with fig.set_panel(panel=1): @@ -90,10 +84,7 @@ ), cmap=True, ) - fig.coast( - land="gray", - shorelines="0.5p,black", - ) + fig.coast(land="gray", shorelines="0.5p,black") # Position the color bar using explicit plot coordinates: # 10 cm is the horizontal center of the 20 cm wide subplot. From 09f57c7eac6a8e9eb1884008d7b30fe60bd7a2fc Mon Sep 17 00:00:00 2001 From: Aayush7788 Date: Tue, 28 Jul 2026 18:14:51 +0530 Subject: [PATCH 03/11] Address additional review comments --- examples/gallery/images/gridding_with_surface.py | 13 ++----------- 1 file changed, 2 insertions(+), 11 deletions(-) diff --git a/examples/gallery/images/gridding_with_surface.py b/examples/gallery/images/gridding_with_surface.py index d7f7319bbd7..49a1bc537c2 100644 --- a/examples/gallery/images/gridding_with_surface.py +++ b/examples/gallery/images/gridding_with_surface.py @@ -37,13 +37,8 @@ ) fig = pygmt.Figure() - pygmt.makecpt(cmap="gmt/geo", series=[-7500, 0, 500]) - -pygmt.config( - FONT_TITLE="12p", - MAP_TITLE_OFFSET="4p", -) +pygmt.config(FONT_TITLE="12p", MAP_TITLE_OFFSET="4p") with fig.subplot( nrows=1, @@ -89,11 +84,7 @@ # Position the color bar using explicit plot coordinates: # 10 cm is the horizontal center of the 20 cm wide subplot. fig.colorbar( - position=Position( - ("10c", "-1.2c"), - cstype="plotcoords", - anchor="TC", - ), + position=Position(("10c", "-1.2c"), cstype="plotcoords", anchor="TC"), length=12, width=0.4, orientation="horizontal", From 936266051045badb3da3c57872c536c6d69c6af8 Mon Sep 17 00:00:00 2001 From: Aayush kotadia <146731463+Aayush7788@users.noreply.github.com> Date: Wed, 29 Jul 2026 14:17:47 +0530 Subject: [PATCH 04/11] Update examples/gallery/images/gridding_with_surface.py MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit updated Co-authored-by: Yvonne Fröhlich <94163266+yvonnefroehlich@users.noreply.github.com> --- examples/gallery/images/gridding_with_surface.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/examples/gallery/images/gridding_with_surface.py b/examples/gallery/images/gridding_with_surface.py index 49a1bc537c2..ee9a8f2af1d 100644 --- a/examples/gallery/images/gridding_with_surface.py +++ b/examples/gallery/images/gridding_with_surface.py @@ -1,6 +1,6 @@ """ -Gridding scattered data with blockmedian and surface -==================================================== +Gridding scattered data +======================= The :func:`pygmt.surface` function interpolates irregularly spaced (x, y, z) observations onto a regular grid. Before interpolation, From d1af0bc0a0e95e294c313aac534759c4de69e234 Mon Sep 17 00:00:00 2001 From: Aayush kotadia <146731463+Aayush7788@users.noreply.github.com> Date: Wed, 29 Jul 2026 14:18:10 +0530 Subject: [PATCH 05/11] Update examples/gallery/images/gridding_with_surface.py MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit updated Co-authored-by: Yvonne Fröhlich <94163266+yvonnefroehlich@users.noreply.github.com> --- examples/gallery/images/gridding_with_surface.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/gallery/images/gridding_with_surface.py b/examples/gallery/images/gridding_with_surface.py index ee9a8f2af1d..cdbf72005f3 100644 --- a/examples/gallery/images/gridding_with_surface.py +++ b/examples/gallery/images/gridding_with_surface.py @@ -17,7 +17,7 @@ data = pygmt.datasets.load_sample_data(name="bathymetry") region = [245, 255, 20, 30] -spacing = "5m" +spacing = "5m" # 5 arc-minutes # Calculate one representative median value for each occupied block. reduced_data = pygmt.blockmedian( From ad0979e242cbd4be033a1211db0a37d2941835d9 Mon Sep 17 00:00:00 2001 From: Aayush kotadia <146731463+Aayush7788@users.noreply.github.com> Date: Wed, 29 Jul 2026 14:19:16 +0530 Subject: [PATCH 06/11] Update examples/gallery/images/gridding_with_surface.py MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit updated Co-authored-by: Yvonne Fröhlich <94163266+yvonnefroehlich@users.noreply.github.com> --- examples/gallery/images/gridding_with_surface.py | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/examples/gallery/images/gridding_with_surface.py b/examples/gallery/images/gridding_with_surface.py index cdbf72005f3..a95b631a5e7 100644 --- a/examples/gallery/images/gridding_with_surface.py +++ b/examples/gallery/images/gridding_with_surface.py @@ -40,12 +40,7 @@ pygmt.makecpt(cmap="gmt/geo", series=[-7500, 0, 500]) pygmt.config(FONT_TITLE="12p", MAP_TITLE_OFFSET="4p") -with fig.subplot( - nrows=1, - ncols=2, - figsize=("20c", "9c"), - margins="0.5c", -): +with fig.subplot(nrows=1, ncols=2, figsize=("20c", "9c")): # Block-median observations. with fig.set_panel(panel=0): fig.basemap( From 516bad27a65e1a42980c061b7745ba108b6ba057 Mon Sep 17 00:00:00 2001 From: Aayush7788 Date: Wed, 29 Jul 2026 15:14:26 +0530 Subject: [PATCH 07/11] Explain surface data bounds --- examples/gallery/images/gridding_with_surface.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/examples/gallery/images/gridding_with_surface.py b/examples/gallery/images/gridding_with_surface.py index a95b631a5e7..92a2dfa1cf2 100644 --- a/examples/gallery/images/gridding_with_surface.py +++ b/examples/gallery/images/gridding_with_surface.py @@ -27,7 +27,8 @@ C=True, ) -# Interpolate the reduced observations onto a regular grid. +# Interpolate the reduced observations onto a regular grid. The value "d" +# constrains the grid to the minimum and maximum input data values. grid = pygmt.surface( data=reduced_data, region=region, From 31a926b402968c83b3d358fa0e4a88c3c4ea9782 Mon Sep 17 00:00:00 2001 From: Aayush7788 Date: Tue, 4 Aug 2026 22:46:50 +0530 Subject: [PATCH 08/11] Use blockmedian center parameter --- examples/gallery/images/gridding_with_surface.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/gallery/images/gridding_with_surface.py b/examples/gallery/images/gridding_with_surface.py index 92a2dfa1cf2..220cacde946 100644 --- a/examples/gallery/images/gridding_with_surface.py +++ b/examples/gallery/images/gridding_with_surface.py @@ -24,7 +24,7 @@ data=data, region=region, spacing=spacing, - C=True, + center=True, ) # Interpolate the reduced observations onto a regular grid. The value "d" From 0f4d230df044d6cbe3af6ad8b33cf6772d30d7fe Mon Sep 17 00:00:00 2001 From: Aayush7788 Date: Sat, 8 Aug 2026 19:53:40 +0530 Subject: [PATCH 09/11] Polish gridding gallery example --- examples/gallery/images/gridding_with_surface.py | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/examples/gallery/images/gridding_with_surface.py b/examples/gallery/images/gridding_with_surface.py index 220cacde946..86c6eca0adc 100644 --- a/examples/gallery/images/gridding_with_surface.py +++ b/examples/gallery/images/gridding_with_surface.py @@ -2,10 +2,11 @@ Gridding scattered data ======================= -The :func:`pygmt.surface` function interpolates irregularly spaced -(x, y, z) observations onto a regular grid. Before interpolation, -:func:`pygmt.blockmedian` reduces redundant observations by returning one -median value for every occupied block. +The :func:`pygmt.surface` function interpolates irregularly spaced (x, y, z) +observations onto a regular grid. Before interpolation, it's usually recommended +to preprocess the data using one of :func:`pygmt.blockmean`, +:func:`pygmt.blockmedian`, and :func:`pygmt.blockmode`, to avoid aliasing short +wavelengths, by returning one value for every occupied block. """ @@ -30,11 +31,7 @@ # Interpolate the reduced observations onto a regular grid. The value "d" # constrains the grid to the minimum and maximum input data values. grid = pygmt.surface( - data=reduced_data, - region=region, - spacing=spacing, - lower="d", - upper="d", + data=reduced_data, region=region, spacing=spacing, lower="d", upper="d" ) fig = pygmt.Figure() From 131c457d03899f384a6b8ba1066ca7c07805e194 Mon Sep 17 00:00:00 2001 From: Aayush7788 Date: Sat, 8 Aug 2026 19:54:45 +0530 Subject: [PATCH 10/11] Polish gridding gallery example --- examples/gallery/images/gridding_with_surface.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/examples/gallery/images/gridding_with_surface.py b/examples/gallery/images/gridding_with_surface.py index 86c6eca0adc..93c5916e094 100644 --- a/examples/gallery/images/gridding_with_surface.py +++ b/examples/gallery/images/gridding_with_surface.py @@ -22,10 +22,7 @@ # Calculate one representative median value for each occupied block. reduced_data = pygmt.blockmedian( - data=data, - region=region, - spacing=spacing, - center=True, + data=data, region=region, spacing=spacing, center=True ) # Interpolate the reduced observations onto a regular grid. The value "d" From de86b1177a7923f2a8f689cdbcf460246379fa33 Mon Sep 17 00:00:00 2001 From: Aayush7788 Date: Tue, 11 Aug 2026 22:14:20 +0530 Subject: [PATCH 11/11] Address gallery review comments --- examples/gallery/images/gridding_with_surface.py | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/examples/gallery/images/gridding_with_surface.py b/examples/gallery/images/gridding_with_surface.py index 93c5916e094..f77ce39067c 100644 --- a/examples/gallery/images/gridding_with_surface.py +++ b/examples/gallery/images/gridding_with_surface.py @@ -3,10 +3,10 @@ ======================= The :func:`pygmt.surface` function interpolates irregularly spaced (x, y, z) -observations onto a regular grid. Before interpolation, it's usually recommended -to preprocess the data using one of :func:`pygmt.blockmean`, -:func:`pygmt.blockmedian`, and :func:`pygmt.blockmode`, to avoid aliasing short -wavelengths, by returning one value for every occupied block. +observations onto a regular grid. Before interpolation, it's usually recommended to +preprocess the data using one of :func:`pygmt.blockmean`, :func:`pygmt.blockmedian`, and +:func:`pygmt.blockmode`, to avoid aliasing short wavelengths, by returning one value for +every occupied block. """ @@ -21,9 +21,7 @@ spacing = "5m" # 5 arc-minutes # Calculate one representative median value for each occupied block. -reduced_data = pygmt.blockmedian( - data=data, region=region, spacing=spacing, center=True -) +reduced_data = pygmt.blockmedian(data=data, region=region, spacing=spacing, center=True) # Interpolate the reduced observations onto a regular grid. The value "d" # constrains the grid to the minimum and maximum input data values. @@ -42,7 +40,6 @@ region=region, projection="M?", frame=Frame( - axes="WSne", axis=Axis(annot=2, tick=1), title="Block-median observations", ), @@ -63,7 +60,6 @@ region=region, projection="M?", frame=Frame( - axes="WSne", axis=Axis(annot=2, tick=1), title="Interpolated surface", ),