Skip to content

Commit

Permalink
plot.imshow now obeys 'origin' kwarg. (#2396)
Browse files Browse the repository at this point in the history
Fixes #2379
  • Loading branch information
dcherian authored Sep 6, 2018
1 parent 73f5b02 commit 66a8f8d
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 6 deletions.
4 changes: 4 additions & 0 deletions doc/whats-new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,10 @@ Enhancements
Bug fixes
~~~~~~~~~

- ``xarray.plot.imshow()`` correctly uses the ``origin`` argument.
(:issue:`2379`)
By `Deepak Cherian <https://github.com/dcherian>`_.

- Fixed ``DataArray.to_iris()`` failure while creating ``DimCoord`` by
falling back to creating ``AuxCoord``. Fixed dependency on ``var_name``
attribute being set.
Expand Down
15 changes: 9 additions & 6 deletions xarray/plot/plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
11 changes: 11 additions & 0 deletions xarray/tests/test_plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down

0 comments on commit 66a8f8d

Please sign in to comment.