Skip to content

imshow should work with third dimension of len 1 #1966

@fmaussion

Description

@fmaussion

Code Sample, a copy-pastable example if possible

import xarray as xr
import numpy as np

da = xr.DataArray(np.arange(9).reshape((1, 3, 3)))

da.plot()  # works
da.plot.imshow()  # fails

Error log:

Details
/home/mowglie/Documents/git/xarray/xarray/plot/utils.py:295: UserWarning: Several dimensions of this array could be colors.  Xarray will use the last possible dimension ('dim_2') to match matplotlib.pyplot.imshow.  You can pass names of x, y, and/or rgb dimensions to override this guess.
  'and/or rgb dimensions to override this guess.' % rgb)

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-7-a0592d3e8758> in <module>()
----> 1 da.plot.imshow()

~/Documents/git/xarray/xarray/plot/plot.py in plotmethod(_PlotMethods_obj, x, y, figsize, size, aspect, ax, row, col, col_wrap, xincrease, yincrease, add_colorbar, add_labels, vmin, vmax, cmap, colors, center, robust, extend, levels, infer_intervals, subplot_kws, cbar_ax, cbar_kwargs, **kwargs)
    679         for arg in ['_PlotMethods_obj', 'newplotfunc', 'kwargs']:
    680             del allargs[arg]
--> 681         return newplotfunc(**allargs)
    682 
    683     # Add to class _PlotMethods

~/Documents/git/xarray/xarray/plot/plot.py in newplotfunc(darray, x, y, figsize, size, aspect, ax, row, col, col_wrap, xincrease, yincrease, add_colorbar, add_labels, vmin, vmax, cmap, center, robust, extend, levels, infer_intervals, colors, subplot_kws, cbar_ax, cbar_kwargs, **kwargs)
    553         rgb = kwargs.pop('rgb', None)
    554         xlab, ylab = _infer_xy_labels(
--> 555             darray=darray, x=x, y=y, imshow=imshow_rgb, rgb=rgb)
    556 
    557         if rgb is not None and plotfunc.__name__ != 'imshow':

~/Documents/git/xarray/xarray/plot/utils.py in _infer_xy_labels(darray, x, y, imshow, rgb)
    308     assert x is None or x != y
    309     if imshow and darray.ndim == 3:
--> 310         return _infer_xy_labels_3d(darray, x, y, rgb)
    311 
    312     if x is None and y is None:

~/Documents/git/xarray/xarray/plot/utils.py in _infer_xy_labels_3d(darray, x, y, rgb)
    297 
    298     # Finally, we pick out the red slice and delegate to the 2D version:
--> 299     return _infer_xy_labels(darray.isel(**{rgb: 0}).squeeze(), x, y)
    300 
    301 

~/Documents/git/xarray/xarray/plot/utils.py in _infer_xy_labels(darray, x, y, imshow, rgb)
    312     if x is None and y is None:
    313         if darray.ndim != 2:
--> 314             raise ValueError('DataArray must be 2d')
    315         y, x = darray.dims
    316     elif x is None:

ValueError: DataArray must be 2d

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions