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

plot.line fails when plot axis is a 1D coordinate #3334

Closed
TomNicholas opened this issue Sep 23, 2019 · 3 comments · Fixed by #3345
Closed

plot.line fails when plot axis is a 1D coordinate #3334

TomNicholas opened this issue Sep 23, 2019 · 3 comments · Fixed by #3345

Comments

@TomNicholas
Copy link
Contributor

TomNicholas commented Sep 23, 2019

MCVE Code Sample

import xarray as xr
import numpy as np

x_coord = xr.DataArray(data=[0.1, 0.2], dims=['x'])
t_coord = xr.DataArray(data=[10, 20], dims=['t'])

da = xr.DataArray(data=np.array([[0, 1], [5, 9]]), dims=['x', 't'],
                  coords={'x': x_coord, 'time': t_coord})
print(da)

da.transpose('time', 'x')

Output:

<xarray.DataArray (x: 2, t: 2)>
array([[0, 1],
       [5, 9]])
Coordinates:
  * x        (x) float64 0.1 0.2
    time     (t) int64 10 20

Traceback (most recent call last):
  File "mwe.py", line 22, in <module>
    da.transpose('time', 'x')
  File "/home/tegn500/Documents/Work/Code/xarray/xarray/core/dataarray.py", line 1877, in transpose
    "permuted array dimensions (%s)" % (dims, tuple(self.dims))
ValueError: arguments to transpose (('time', 'x')) must be permuted array dimensions (('x', 't'))

As 'time' is a coordinate with only one dimension, this is an unambiguous operation that I want to perform. However, because .transpose() currently only accepts dimensions, this fails with that error.

This causes bug in other parts of the code - for example I found this by trying to plot this type of dataarray:

da.plot(x='time', hue='x')

which gives the same error.

(You can get a similar error also with da.plot(y='time', hue='x').)

If the code which explicitly checks that the arguments to transpose are dims and not just coordinate dimensions is removed, then both of these examples work as expected.

I would like to generalise the transpose function to also accept dimension coordinates, is there any reason not to do this?

@dcherian
Copy link
Contributor

I am unsure about this change but the plot statement does work on my end.

@TomNicholas
Copy link
Contributor Author

Ah that's because I made a typo above. If you change it to da.plot(x='time', hue='x') then it should fail with master.

@TomNicholas TomNicholas changed the title Should transpose accept coordinate dimensions? plot.line fails when plot axis is a 1D coordinate Sep 25, 2019
@TomNicholas
Copy link
Contributor Author

So instead of changing transpose (which opens a can of worms) I've just made a small change in plot.line so that it can handle this case.

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

Successfully merging a pull request may close this issue.

2 participants