Skip to content

Add DataArray.gmt.imshow() function to show 2D DataArray easily #2372

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

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft
Changes from all 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
23 changes: 23 additions & 0 deletions pygmt/accessors.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import xarray as xr
from pygmt.exceptions import GMTInvalidInput
from pygmt.figure import Figure
from pygmt.src.grdinfo import grdinfo


Expand Down Expand Up @@ -165,3 +166,25 @@ def gtype(self, value):
"either 0 for Cartesian or 1 for Geographic."
)
self._gtype = value

def imshow(self, fig=None):
"""
Image plot of 2D :class:`xarray.DataArray`.

Examples
--------

>>> from pygmt.datasets import load_earth_relief
>>> grid = load_earth_relief()
>>> grid.gmt.imshow()
"""
newfig = False
if fig is None:
fig = Figure()
newfig = True
fig.grdimage(self._obj, frame=True)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we do imshow(self, *kwargs) and allow users to pass extra keyword arguments to grdimage?

fig.colorbar(frame=True)

if newfig:
fig.show()
return fig