Skip to content

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

Closed
@TomNicholas

Description

@TomNicholas

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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions