Description
Pygmt incorrectly plots xarrays when using cylindrical projections ('Q').
This is not an issue with gmt: if the dataarray is first exported to a netcdf file, pygmt works as intended when using the file. Thus, this appears to be a problem with how pygmt treats internal xarrays, and only for the case of cylindrical projections (the most basic of all the projections!). This is demonstrated in the following script.
The script generates a simple dataset, and the plots the following:
- upper row: 'Q0/0/6i' and 'Q180/0/6i' reading data from a file.
- middle row: 'W0/6i' and 'W180/6i' using internal xarray.
- bottom row: 'Q0/0/6i' and 'Q180/0/6i' using internal xarray.
As you see, the upper two rows plot the data correctly. However, for the bottom left image, the data are not plotted with the correct central_longitude
. Furthermore, there is a problem with the first longitude band not being plotted.
It is possible that this is related to this issue: #375
# Creata a data array in gridline coordinates of sin(lon) * cos(lat)
interval = 10
lat = np.arange(90, -90-interval, -interval)
lon = np.arange(0, 360+interval, interval)
longrid, latgrid = np.meshgrid(lon, lat)
data = np.sin(np.deg2rad(longrid)) * np.cos(np.deg2rad(latgrid))
# create a DataArray
dataarray = xr.DataArray(data, coords=[('latitude', lat,
{'units': 'degrees_north'}),
('longitude', lon,
{'units': 'degrees_east'})],
attrs = {'actual_range': [-1, 1]})
dataset = dataarray.to_dataset(name='dataarray')
dataset.to_netcdf('test.grd')
# create projected images using a cylindrical and mollweide projection.
fig = pygmt.Figure()
fig.grdimage(dataset.dataarray, region='g', projection='Q0/0/6i', frame='a90f30g30')
fig.shift_origin(xshift='7i')
fig.grdimage(dataset.dataarray, region='g', projection='Q180/0/6i', frame='a90f30g30')
fig.shift_origin(xshift='-7i', yshift='4i')
fig.grdimage(dataset.dataarray, region='g', projection='W0/6i', frame='a90f30g30')
fig.shift_origin(xshift='7i')
fig.grdimage(dataset.dataarray, region='g', projection='W180/6i', frame='a90f30g30')
fig.shift_origin(xshift='-7i', yshift='4i')
fig.grdimage('test.grd', region='g', projection='Q0/0/6i', frame='a90f30g30')
fig.shift_origin(xshift='7i')
fig.grdimage('test.grd', region='g', projection='Q180/0/6i', frame='a90f30g30')
fig.savefig('test.png')
fig.show()