Skip to content

Commit

Permalink
override the signature of plotfunc (#4359)
Browse files Browse the repository at this point in the history
* override the signature of plotfunc

* update whats-new.rst

* explain the reasons for the signature dummy
  • Loading branch information
keewis authored Sep 6, 2020
1 parent a6eccfa commit 5d6a734
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
1 change: 1 addition & 0 deletions doc/whats-new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ Bug fixes
and :py:meth:`DataArray.str.wrap` (:issue:`4334`). By `Mathias Hauser <https://github.com/mathause>`_.
- Fixed overflow issue causing incorrect results in computing means of :py:class:`cftime.datetime`
arrays (:issue:`4341`). By `Spencer Clark <https://github.com/spencerkclark>`_.
- fix the signature of the plot methods. (:pull:`4359`) By `Justus Magin <https://github.com/keewis>`_.
- Fix :py:func:`xarray.apply_ufunc` with ``vectorize=True`` and ``exclude_dims`` (:issue:`3890`).
By `Mathias Hauser <https://github.com/mathause>`_.
- Fix `KeyError` when doing linear interpolation to an nd `DataArray`
Expand Down
19 changes: 19 additions & 0 deletions xarray/plot/plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -463,6 +463,15 @@ def step(self, *args, **kwargs):
return step(self._da, *args, **kwargs)


def override_signature(f):
def wrapper(func):
func.__wrapped__ = f

return func

return wrapper


def _plot2d(plotfunc):
"""
Decorator for common 2d plotting logic
Expand Down Expand Up @@ -572,6 +581,16 @@ def _plot2d(plotfunc):
# Build on the original docstring
plotfunc.__doc__ = f"{plotfunc.__doc__}\n{commondoc}"

# plotfunc and newplotfunc have different signatures:
# - plotfunc: (x, y, z, ax, **kwargs)
# - newplotfunc: (darray, x, y, **kwargs)
# where plotfunc accepts numpy arrays, while newplotfunc accepts a DataArray
# and variable names. newplotfunc also explicitly lists most kwargs, so we
# need to shorten it
def signature(darray, x, y, **kwargs):
pass

@override_signature(signature)
@functools.wraps(plotfunc)
def newplotfunc(
darray,
Expand Down

0 comments on commit 5d6a734

Please sign in to comment.