Skip to content

Commit fd819a9

Browse files
authored
Avoid setting 1D x,y where that dimension clashes with hue. (#298)
1 parent e240e51 commit fd819a9

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

cf_xarray/accessor.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -765,7 +765,15 @@ def _get_possible(accessor, criteria):
765765
elif ax_coord_name:
766766
values = [v for v in values if v in ax_coord_name]
767767

768-
values = [v for v in values if v != skip]
768+
if skip is not None:
769+
skipvar = obj.cf[skip]
770+
bad_names = (skip, skipvar.name)
771+
bad_dims = ((skip,), skipvar.dims)
772+
values = [
773+
v
774+
for v in values
775+
if v not in bad_names and obj[v].dims not in bad_dims
776+
]
769777
if len(values) == 1 and not is_scalar(accessor._obj[values[0]]):
770778
return values[0]
771779
else:

cf_xarray/tests/test_accessor.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1223,6 +1223,14 @@ def makeds(*dims):
12231223
assert _possible_x_y_plot(xtds, "y") is None
12241224
assert _possible_x_y_plot(xtds, "x") == "X"
12251225

1226+
xtds.coords["lon"] = ("X", [1, 2, 3], {"standard_name": "longitude"})
1227+
# skip lon (which is 1D on X) if user passes hue="X"
1228+
# choose T instead which is a different dimension
1229+
# (and so a more meaningful plot)
1230+
assert _possible_x_y_plot(xtds, "x", skip="X") == "T"
1231+
# now with hue="lon", skip "X"
1232+
assert _possible_x_y_plot(xtds, "x", skip="lon") == "T"
1233+
12261234

12271235
def test_groupby_special_ops():
12281236
cfgrouped = airds.cf.groupby_bins("latitude", np.arange(20, 50, 10))

0 commit comments

Comments
 (0)