Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add gallery example to showcase blockmean #1598

Merged
merged 16 commits into from
Nov 21, 2021
Merged
Changes from 5 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
62 changes: 62 additions & 0 deletions examples/gallery/histograms/blockm.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
"""
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()

# ----------------------------------------------------
michaelgrund marked this conversation as resolved.
Show resolved Hide resolved
# Calculate mean depth in km from all events within 150x 150
# bins using blockmean
df = pygmt.blockmean(data, region=region, spacing=spacing)
michaelgrund marked this conversation as resolved.
Show resolved Hide resolved
# convert to grid
grd = pygmt.xyz2grd(df, region=region, spacing=spacing)
michaelgrund marked this conversation as resolved.
Show resolved Hide resolved

fig.grdimage(
grd,
michaelgrund marked this conversation as resolved.
Show resolved Hide resolved
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")
michaelgrund marked this conversation as resolved.
Show resolved Hide resolved
# 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,
michaelgrund marked this conversation as resolved.
Show resolved Hide resolved
region=region,
frame=["af", '+t"Number of points inside each block"'],
cmap="batlow",
)
fig.coast(land="darkgray", transparency="40")
michaelgrund marked this conversation as resolved.
Show resolved Hide resolved
fig.plot(
x=data.longitude, y=data.latitude, style="c0.3c", color="white", pen="1p,black"
)
fig.colorbar(frame=["x+lcount"])

fig.show()