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

Plotting with 2d coordinates, error in transposed coordinate #3138

Closed
hetland opened this issue Jul 17, 2019 · 3 comments · Fixed by #3441
Closed

Plotting with 2d coordinates, error in transposed coordinate #3138

hetland opened this issue Jul 17, 2019 · 3 comments · Fixed by #3441

Comments

@hetland
Copy link

hetland commented Jul 17, 2019

This is similar to issue #1503, but specifically deals with requiring the correct order of dimensions for plotting 2d coordinates. An example of the issue is:

# create an example dataset
dates = pd.date_range('2000-01-01', '2001-12-31', name='time')
times = dates - dates[0]

x = np.linspace(0, 10, 101)

h = np.linspace(3, 7, 101)
s = np.linspace(0, 1, 51)
z = s[:, np.newaxis] * h[np.newaxis, :]

data = (np.sin(x) * np.cos(z)) * np.cos(np.asarray(times.days[:, np.newaxis, np.newaxis]))

# create an xarray dataset

ds = xr.Dataset({'data': (('time', 's', 'x'), data)}, 
                {'time':dates, 'x':x, 's':s, 'z':(('s', 'x'), z)})
ds.coords['zt'] = ds.z.transpose()

plotting with z and zt should give the same results, since the dimensions are clearly labeled and other than the order of the dimensions, they are exactly the same.

This works:

ds.data[0].plot(x='x', y='z')

This does not:

ds.data[0].plot(x='x', y='zt')

Broadcasting, e.g., does not give a similar error. Specifically, this is true:

np.allclose((ds.data * ds.zt).values, (ds.data * ds.z).values)
@shoyer
Copy link
Member

shoyer commented Jul 17, 2019

Thanks for the report.

I think should be relatively straightforward to fix, assuming that the underlying maptlotlib plotting functions handle 2D coordinates.

For reference, here's the full error traceback:

ValueError                                Traceback (most recent call last)
<ipython-input-6-8d3b2b196427> in <module>()
----> 1 ds.data[0].plot.pcolormesh(x='x', y='zt')

3 frames
/usr/local/lib/python3.6/dist-packages/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, xscale, yscale, xticks, yticks, xlim, ylim, norm, **kwargs)
    697         for arg in ['_PlotMethods_obj', 'newplotfunc', 'kwargs']:
    698             del allargs[arg]
--> 699         return newplotfunc(**allargs)
    700 
    701     # Add to class _PlotMethods

/usr/local/lib/python3.6/dist-packages/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, xscale, yscale, xticks, yticks, xlim, ylim, norm, **kwargs)
    583         # check if we need to broadcast one dimension
    584         if xval.ndim < yval.ndim:
--> 585             xval = np.broadcast_to(xval, yval.shape)
    586 
    587         if yval.ndim < xval.ndim:

/usr/local/lib/python3.6/dist-packages/numpy/lib/stride_tricks.py in broadcast_to(array, shape, subok)
    180            [1, 2, 3]])
    181     """
--> 182     return _broadcast_to(array, shape, subok=subok, readonly=True)
    183 
    184 

/usr/local/lib/python3.6/dist-packages/numpy/lib/stride_tricks.py in _broadcast_to(array, shape, subok, readonly)
    127     it = np.nditer(
    128         (array,), flags=['multi_index', 'refs_ok', 'zerosize_ok'] + extras,
--> 129         op_flags=[op_flag], itershape=shape, order='C')
    130     with it:
    131         # never really has writebackifcopy semantics

ValueError: operands could not be broadcast together with remapped shapes [original->remapped]: (101,) and requested shape (101,51)

@dcherian
Copy link
Contributor

Thanks @hetland, good to see you here.

I think this is probably easy to fix by using xr.broadcast to broadcast things before extracting the numpy arrays. Can you send in a PR?

@hetland
Copy link
Author

hetland commented Jul 17, 2019 via email

@dcherian dcherian pinned this issue Aug 22, 2019
dcherian added a commit to dcherian/xarray that referenced this issue Oct 24, 2019
@dcherian dcherian unpinned this issue Oct 24, 2019
dcherian added a commit to dcherian/xarray that referenced this issue Oct 24, 2019
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.

3 participants