Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

imshow should work with third dimension of len 1 #1966

Closed
fmaussion opened this issue Mar 6, 2018 · 2 comments · Fixed by #1967
Closed

imshow should work with third dimension of len 1 #1966

fmaussion opened this issue Mar 6, 2018 · 2 comments · Fixed by #1967

Comments

@fmaussion
Copy link
Member

fmaussion commented Mar 6, 2018

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:

/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
@fmaussion
Copy link
Member Author

@Zac-HD , do you think you can have a look? Otherwise I can have a look too but it might take a while.

@Zac-HD
Copy link
Contributor

Zac-HD commented Mar 6, 2018

Ugh, this is literally a one-line fix: I shouldn't have put .squeeze() here. If you also ensure that the dtype is OK (specify uint8, or divide by nine) the example then works.

It's also exactly the kind of thing that Hypothesis would catch for us via #1846. Work (moved to less coding 😢) and the upcoming major release of Hypothesis have priority, but I'll try to find time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants