You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I called a 2-D plot method with one coordinate specified as a non-dimension coordinate through the x or y argument. The method raised a ValueError that complained about a tuple of duplicate coordinates that I had not provided.
Also, when I specified x and omitted y, the method took y to be the second dimension of the DataArray.
What you expected to happen:
I expected the unspecified x or y argument to behave as when I specified neither argument, so that, for a suitable DataArrayda, calling da.plot(x="lon") would have the same Y axis as da.plot(). That expectation is consistent with the docstring common to 2-D plotting methods, where the default values for the x and y arguments are da.dims[1] and da.dims[0], respectively. However, as discussed below, the docstring does not mention that xarray tries to guess the omitted coordinate.
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "...\xarray\plot\plot.py", line 446, in __call__
return plot(self._da, **kwargs)
File "...\xarray\plot\plot.py", line 200, in plot
return plotfunc(darray, **kwargs)
File "...\xarray\plot\plot.py", line 707, in newplotfunc
darray = darray.transpose(*dims, transpose_coords=True)
File "...\xarray\core\dataarray.py", line 2036, in transpose
dims = tuple(utils.infix_dims(dims, self.dims))
File "...\xarray\core\utils.py", line 725, in infix_dims
raise ValueError(
ValueError: ('u', 'u') must be a permuted list of ('v', 'u'), unless `...` is included
Executing da.plot(y="lon") causes the same exception.
Anything else we need to know?:
Two aspects of this issue that I see are the behavior and the documentation.
The behavior in the above example involves xarray trying to guess the other dimension [#1291 in v. 0.9.2]. The guessing logic tests whether the given coordinate equals one of the array's dimension names; here, the given coordinate is a non-dimension coordinate and fails to match. When the match fails, the other coordinate is set to the second dimension, whether the given coordinate was x or y. One possible improvement would be to set the y coordinate to the first dimension instead, as in the following replacement for the last line of the guessing logic:
With that change, da.plot(x="lon") and da.plot(y="lat") would succeed and produce the plots I expect, while da.plot(x="lat") and da.plot(y="lon") would raise ValueError. Also, I would find it more helpful if the exception message were more clearly related to the arguments of the call. The _infer_xy_labels function might need to notice that it was about to set x and y to the same dimension name and raise an exception at that point.
The documentation seems not to have been updated for #1291 and does not mention the guessing behavior.
Thanks for the clear report and the example! Note that da.plot(x="lon", y="lat") and da.plot(x="lon", y="v") both work. So you have at least a workaround. But yes I think it would be good to extend the logic to work when a non-dimension coordinate is passed.
Note that da.plot(x="lon", y="lat") and da.plot(x="lon", y="v") both work. So you have at least a workaround.
Yes. I didn't readily see that I needed to specify both x and y, partly because of the documentation and partly because I was deceived by the traceback. (Although xr.core.utils.infix_dims() raises the exception, xr.plot.utils._infer_xy_labels() causes the problem, and the responsible calls are separated by tens of lines in xr.plot.plot._plot2d().
But yes I think it would be good to extend the logic to work when a non-dimension coordinate is passed.
I agree that, if xarray is going to guess, handling non-dimension coordinates would be an improvement. It might be specified to work only with 1-D coordinates and to do nothing for multi-dimensional coordinates.
Alternatively, is it at all preferable to remove the guessing feature? Although the behavior has been present for years, I found mention of it in the documentation only in "What's New." The motivating use case in #1290 could have been addressed instead simply with z.transpose().plot(), which the guessing causes under the hood anyway.
What happened:
I called a 2-D plot method with one coordinate specified as a non-dimension coordinate through the
x
ory
argument. The method raised aValueError
that complained about a tuple of duplicate coordinates that I had not provided.Also, when I specified
x
and omittedy
, the method tooky
to be the second dimension of theDataArray
.What you expected to happen:
I expected the unspecified
x
ory
argument to behave as when I specified neither argument, so that, for a suitableDataArray
da
, callingda.plot(x="lon")
would have the same Y axis asda.plot()
. That expectation is consistent with the docstring common to 2-D plotting methods, where the default values for thex
andy
arguments areda.dims[1]
andda.dims[0]
, respectively. However, as discussed below, the docstring does not mention that xarray tries to guess the omitted coordinate.Minimal Complete Verifiable Example:
Executing
causes (with file paths abbreviated)
Executing
da.plot(y="lon")
causes the same exception.Anything else we need to know?:
Two aspects of this issue that I see are the behavior and the documentation.
The behavior in the above example involves xarray trying to guess the other dimension [#1291 in v. 0.9.2]. The guessing logic tests whether the given coordinate equals one of the array's dimension names; here, the given coordinate is a non-dimension coordinate and fails to match. When the match fails, the other coordinate is set to the second dimension, whether the given coordinate was
x
ory
. One possible improvement would be to set they
coordinate to the first dimension instead, as in the following replacement for the last line of the guessing logic:With that change,
da.plot(x="lon")
andda.plot(y="lat")
would succeed and produce the plots I expect, whileda.plot(x="lat")
andda.plot(y="lon")
would raiseValueError
. Also, I would find it more helpful if the exception message were more clearly related to the arguments of the call. The_infer_xy_labels
function might need to notice that it was about to setx
andy
to the same dimension name and raise an exception at that point.The documentation seems not to have been updated for #1291 and does not mention the guessing behavior.
Environment:
Output of xr.show_versions()
The text was updated successfully, but these errors were encountered: