Skip to content
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
104 changes: 104 additions & 0 deletions examples/gallery/images/gridding_with_surface.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
"""
Gridding scattered data with blockmedian and surface
====================================================
Comment thread
Aayush7788 marked this conversation as resolved.
Outdated

The :func:`pygmt.surface` function interpolates irregularly spaced
Comment thread
seisman marked this conversation as resolved.
Outdated
(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.
Comment thread
seisman marked this conversation as resolved.
Outdated

Comment thread
seisman marked this conversation as resolved.
"""

# %%
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"
Comment thread
Aayush7788 marked this conversation as resolved.
Outdated

# Calculate one representative median value for each occupied block.
reduced_data = pygmt.blockmedian(
data=data,
region=region,
spacing=spacing,
C=True,
Comment thread
seisman marked this conversation as resolved.
Outdated
)

# Interpolate the reduced observations onto a regular grid.
grid = pygmt.surface(
data=reduced_data,
region=region,
spacing=spacing,
lower="d",
upper="d",
Comment thread
yvonnefroehlich marked this conversation as resolved.
Outdated
)
Comment thread
seisman marked this conversation as resolved.

fig = pygmt.Figure()

pygmt.makecpt(cmap="gmt/geo", series=[-7500, 0, 500])

pygmt.config(
FONT_TITLE="12p",
MAP_TITLE_OFFSET="4p",
)

Comment thread
yvonnefroehlich marked this conversation as resolved.
with fig.subplot(
nrows=1,
ncols=2,
figsize=("20c", "9c"),
margins="0.5c",
):
Comment thread
Aayush7788 marked this conversation as resolved.
Outdated
# Block-median observations.
with fig.set_panel(panel=0):
fig.basemap(
region=region,
projection="M?",
frame=Frame(
axes="WSne",
Comment thread
seisman marked this conversation as resolved.
Outdated
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",
Comment thread
seisman marked this conversation as resolved.
Outdated
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",
),
Comment thread
yvonnefroehlich marked this conversation as resolved.
Outdated
length=12,
width=0.4,
orientation="horizontal",
annot=1000,
label="Bathymetry (m)",
)

fig.show()