Skip to content

pygmt.grdmask: Improve docs for "inside", "outside" and "edge" parameters #4528

@yvonnefroehlich

Description

@yvonnefroehlich

Description of the problem

pygmt.grdmask allows to cut out polygons from a grid. Using inside= "NaN" sets the nodes inside the grid to NaN. However the nodes outside of the polygon are changed to zero (right map). This is not the case for using outside="NaN": Here the nodes outside the polygon are set to NaN and the nodes inside the polygon are unchanged (middle map).

Image

Minimal Complete Verifiable Example

import numpy as np
import pygmt

region = [125, 135, 25, 36]
polygon = np.array([[129, 31], [134, 31],  [134, 35],  [129, 35],  [129, 31],])

grid = pygmt.datasets.load_earth_relief(region=region, resolution="30s")

mask_out = pygmt.grdmask(region=region, data=polygon, spacing="30s", outside="NaN")
mask_in = pygmt.grdmask(region=region, data=polygon, spacing="30s", inside="NaN")
grid_mask_out = grid * mask_out
grid_mask_in = grid * mask_in

args_basemap = {"region": region, "projection": "M12c", "frame": True}


fig = pygmt.Figure()
pygmt.makecpt(cmap="oleron", series=[-2000, 2000])

# Plot the elevation grid
fig.basemap(**args_basemap)
fig.grdimage(grid=grid, cmap=True)

fig.shift_origin(xshift="+w+2c")

# Plot the masked elevation grid outside
fig.basemap(**args_basemap)
fig.grdimage(grid=grid_mask_out, cmap=True)

fig.shift_origin(xshift="+w+2c")

# Plot the masked elevation grid inside
fig.basemap(**args_basemap)
fig.grdimage(grid=grid_mask_in, cmap=True)

fig.colorbar(frame=True)
fig.show()

Full error message

No error message occurrs. For the right map, the elevation grid should remain unchanged in the area outside the square.

System information

pygmt.show_versions()
PyGMT information:
  version: v0.19.0.dev151
System information:
  python: 3.14.2 | packaged by conda-forge | (main, Dec  6 2025, 11:17:27) [MSC v.1944 64 bit (AMD64)]
  executable: C:\ProgramData\Anaconda3\envs\pygmt_env_dev\python.exe
  machine: Windows-10-10.0.19045-SP0
Dependency information:
  numpy: 2.4.1
  pandas: 2.3.3
  xarray: 2025.12.0
  packaging: 25.0
  contextily: 1.7.0
  geopandas: 1.1.2
  IPython: 9.9.0
  pyarrow: 22.0.0
  rioxarray: 0.20.0
  gdal: 3.12.1
  ghostscript: 10.06.0
GMT library information:
  version: 6.6.0
  padding: 2
  share dir: C:/Program Files (x86)/gmt6/share
  plugin dir: C:/ProgramData/Anaconda3/envs/pygmt_env_dev/Library/bin/gmt_plugins
  library path: C:/ProgramData/Anaconda3/envs/pygmt_env_dev/Library/bin/gmt.dll
  cores: 4
  grid layout: rows
  image layout: 
  binary version: 6.6.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    documentationImprovements or additions to documentation

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions