Skip to content

problems with grdcontour when using xarray DataArrays #3479

Open

Description

@MarkWieczorek

Description of the problem

I am attempting to draw a contour which encompasses the south pole using grdcontour. Everything works fine when passing a netcdf filename, but when passing an xarray DataArray, the points at the far left and right of the plot are connected by a straight line.

Here is the image using a DataArray
grid1

And here is the image using the netcdf file
grid2

I note that the second image generates a warning (as shown below), but it works nonetheless.

Minimal Complete Verifiable Example

# create a dummy dataset
interval = 0.2
lat = np.arange(90, -90 - interval, -interval)
lon = np.arange(0, 360 + interval, interval)
longrid, latgrid = np.meshgrid(lon, lat)
data = 0.2 * np.cos(np.deg2rad(longrid)) + np.sin(np.deg2rad(latgrid))
grid = xr.DataArray(data, coords=[("latitude", lat), ("longitude", lon)])
grid.gmt.registration = 0
grid.gmt.gtype = 1
grid.to_netcdf('grid.nc')

# plot and use grdcontour with the dataarray
fig = pygmt.Figure()
fig.grdimage(grid, projection='W0/10c')
fig.grdcontour(grid, levels='-0.9,')
fig.show()
fig.savefig('grid1.png')

# Do the same using the netcdf file
fig2 = pygmt.Figure()
fig2.grdimage('grid.nc', projection='W0/10c')
fig2.grdcontour('grid.nc', levels='-0.9,')
fig2.show()
fig2.savefig('grid2.png')

Full error message

grdimage [WARNING]: Guessing of registration in conflict between x and y, using gridline
grdcontour [WARNING]: Guessing of registration in conflict between x and y, using gridline

System information

PyGMT information:
  version: v0.13.0
System information:
  python: 3.12.5 | packaged by conda-forge | (main, Aug  8 2024, 18:36:51) [GCC 12.4.0]
  executable: /home/mrak/miniforge3/envs/py312/bin/python
  machine: Linux-6.10.10-200.fc40.x86_64-x86_64-with-glibc2.39
Dependency information:
  numpy: 2.1.0
  pandas: 2.2.2
  xarray: 2024.7.0
  netCDF4: 1.7.1
  packaging: 24.1
  contextily: None
  geopandas: None
  IPython: 8.26.0
  rioxarray: None
  gdal: 3.9.2
  ghostscript: 10.03.1
GMT library information:
  version: 6.5.0
  padding: 2
  share dir: /home/mrak/miniforge3/envs/py312/share/gmt
  plugin dir: /home/mrak/miniforge3/envs/py312/lib/gmt/plugins
  library path: /home/mrak/miniforge3/envs/py312/lib/libgmt.so
  cores: 20
  grid layout: rows
  image layout: 
  binary version: 6.5.0

Activity

  1. added
    bug
    on Oct 1, 2024
  2. leouieda commented on Sep 5, 2025

    @leouieda
    Member

    Just reporting here that I had the same issue with contours wrapping around in a straight line. I think this may have something to do with loss of some metadata when we pass the grid as a DataArray but I'm not sure.

  3. yousizaitianqiong commented on Nov 21, 2026

    @yousizaitianqiong
    Contributor

    I reproduced the extra line by calling the GMT C API directly on GMT master (97962cf3, version 6.8.0). I used a 1° global grid with geographic coordinates and gridline registration explicitly set. With a Mollweide projection centered at 0° and the -0.9 contour, the 0°–360° grid passed as GMT_VIA_MATRIX produced the long line. A NetCDF file and a GMT_GRID virtual file produced identical plots. Aligning the matrix longitudes to -180°–180° also removed the long line in this example. The column with the most contour pixels contained 959 pixels for the unaligned matrix and 116 for each of the three controls.

    GMT detects the seam crossing in this case. Rounding in a projected coordinate can reverse the order of the two boundary intersections and draw a segment across the map. A GMT-side candidate uses the crossing direction when ordering the intersections and preserves it through latitude clipping. The unaligned matrix measures 116 pixels in the local rerun, and cropped Mollweide and Hammer cases also pass their drawing comparisons. Further validation is in progress.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions