diff --git a/doc/whats-new.rst b/doc/whats-new.rst index 98fb955ac48..881ae52cdeb 100644 --- a/doc/whats-new.rst +++ b/doc/whats-new.rst @@ -79,6 +79,10 @@ Enhancements Bug fixes ~~~~~~~~~ +- ``xarray.plot.imshow()`` correctly uses the ``origin`` argument. + (:issue:`2379`) + By `Deepak Cherian `_. + - Fixed ``DataArray.to_iris()`` failure while creating ``DimCoord`` by falling back to creating ``AuxCoord``. Fixed dependency on ``var_name`` attribute being set. diff --git a/xarray/plot/plot.py b/xarray/plot/plot.py index 0b3ab6f1bde..10fca44b417 100644 --- a/xarray/plot/plot.py +++ b/xarray/plot/plot.py @@ -624,7 +624,7 @@ def _plot2d(plotfunc): @functools.wraps(plotfunc) def newplotfunc(darray, x=None, y=None, figsize=None, size=None, aspect=None, ax=None, row=None, col=None, - col_wrap=None, xincrease=True, yincrease=True, + col_wrap=None, xincrease=None, yincrease=None, add_colorbar=None, add_labels=True, vmin=None, vmax=None, cmap=None, center=None, robust=False, extend=None, levels=None, infer_intervals=None, colors=None, @@ -794,7 +794,7 @@ def newplotfunc(darray, x=None, y=None, figsize=None, size=None, @functools.wraps(newplotfunc) def plotmethod(_PlotMethods_obj, x=None, y=None, figsize=None, size=None, aspect=None, ax=None, row=None, col=None, col_wrap=None, - xincrease=True, yincrease=True, add_colorbar=None, + xincrease=None, yincrease=None, add_colorbar=None, add_labels=True, vmin=None, vmax=None, cmap=None, colors=None, center=None, robust=False, extend=None, levels=None, infer_intervals=None, subplot_kws=None, @@ -862,10 +862,8 @@ def imshow(x, y, z, ax, **kwargs): left, right = x[0] - xstep, x[-1] + xstep bottom, top = y[-1] + ystep, y[0] - ystep - defaults = {'extent': [left, right, bottom, top], - 'origin': 'upper', - 'interpolation': 'nearest', - } + defaults = {'origin': 'upper', + 'interpolation': 'nearest'} if not hasattr(ax, 'projection'): # not for cartopy geoaxes @@ -874,6 +872,11 @@ def imshow(x, y, z, ax, **kwargs): # Allow user to override these defaults defaults.update(kwargs) + if defaults['origin'] == 'upper': + defaults['extent'] = [left, right, bottom, top] + else: + defaults['extent'] = [left, right, top, bottom] + if z.ndim == 3: # matplotlib imshow uses black for missing data, but Xarray makes # missing data transparent. We therefore add an alpha channel if diff --git a/xarray/tests/test_plot.py b/xarray/tests/test_plot.py index c38ffeff884..15cb6af5fb1 100644 --- a/xarray/tests/test_plot.py +++ b/xarray/tests/test_plot.py @@ -1308,6 +1308,17 @@ def test_regression_rgb_imshow_dim_size_one(self): da = DataArray(easy_array((1, 3, 3), start=0.0, stop=1.0)) da.plot.imshow() + def test_imshow_origin_kwarg(self): + da = DataArray(easy_array((5, 5, 3), start=-0.6, stop=1.4)) + da.plot.imshow(origin='upper') + assert plt.xlim()[0] < 0 + assert plt.ylim()[1] < 0 + + plt.clf() + da.plot.imshow(origin='lower') + assert plt.xlim()[0] < 0 + assert plt.ylim()[0] < 0 + class TestFacetGrid(PlotTestCase): def setUp(self):