From 5d6a7348d295d26347be3a91073d81f810b6558e Mon Sep 17 00:00:00 2001 From: keewis Date: Mon, 7 Sep 2020 01:05:03 +0200 Subject: [PATCH] override the signature of plotfunc (#4359) * override the signature of plotfunc * update whats-new.rst * explain the reasons for the signature dummy --- doc/whats-new.rst | 1 + xarray/plot/plot.py | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/doc/whats-new.rst b/doc/whats-new.rst index eda5c9dfbf6..316254a97d7 100644 --- a/doc/whats-new.rst +++ b/doc/whats-new.rst @@ -77,6 +77,7 @@ Bug fixes and :py:meth:`DataArray.str.wrap` (:issue:`4334`). By `Mathias Hauser `_. - Fixed overflow issue causing incorrect results in computing means of :py:class:`cftime.datetime` arrays (:issue:`4341`). By `Spencer Clark `_. +- fix the signature of the plot methods. (:pull:`4359`) By `Justus Magin `_. - Fix :py:func:`xarray.apply_ufunc` with ``vectorize=True`` and ``exclude_dims`` (:issue:`3890`). By `Mathias Hauser `_. - Fix `KeyError` when doing linear interpolation to an nd `DataArray` diff --git a/xarray/plot/plot.py b/xarray/plot/plot.py index 305405d4e5a..0d44d5fd64a 100644 --- a/xarray/plot/plot.py +++ b/xarray/plot/plot.py @@ -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 @@ -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,