Skip to content

Commit ad61ad2

Browse files
committed
⏺ Fixed the review issues:
1. Notebook comment - Clarified that to_array() returns (variable, ...) but xpx() reorders it 2. Negative indexing - Now supports Python-style: -1=last, -2=second-to-last, etc. 3. Import - Moved _options to module level
1 parent 7beece7 commit ad61ad2

File tree

2 files changed

+10
-9
lines changed

2 files changed

+10
-9
lines changed

docs/examples/datasets.ipynb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,9 @@
110110
"outputs": [],
111111
"source": [
112112
"# Default: position=1 (second) -> variable goes to color\n",
113-
"# Dimensions are: (time, variable, city)\n",
114-
"print(\"Default dimension order:\", ds.to_array().dims)\n",
113+
"# Note: to_array() puts \"variable\" first, but xpx() reorders it to position 1\n",
114+
"print(\"Raw to_array() dims:\", ds.to_array().dims) # (variable, time, city)\n",
115+
"print(\"After xpx reorder: (time, variable, city)\") # time->x, variable->color\n",
115116
"xpx(ds).line(title=\"Default: variable as color (position=1)\")"
116117
]
117118
},

xarray_plotly/accessor.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
from xarray_plotly import plotting
99
from xarray_plotly.common import SlotValue, auto
10+
from xarray_plotly.config import _options
1011

1112

1213
class DataArrayPlotlyAccessor:
@@ -353,21 +354,20 @@ def _get_dataarray(self, var: str | None) -> DataArray:
353354
354355
When combining all variables, "variable" is placed at the position
355356
specified by config.dataset_variable_position (default 1, second position).
357+
Supports Python-style negative indexing: -1 = last, -2 = second-to-last, etc.
356358
"""
357359
if var is None:
358-
from xarray_plotly.config import _options
359-
360360
da = self._ds.to_array(dim="variable")
361361
pos = _options.dataset_variable_position
362362
# Move "variable" to configured position
363363
if len(da.dims) > 1 and pos != 0:
364364
dims = list(da.dims)
365365
dims.remove("variable")
366-
# Handle negative indices and bounds
367-
if pos < 0:
368-
dims.append("variable")
369-
else:
370-
dims.insert(min(pos, len(dims)), "variable")
366+
# Use Python-style indexing (handles negative indices correctly)
367+
# Clamp to valid range: -1 -> last, -2 -> second-to-last, etc.
368+
n = len(dims)
369+
insert_pos = max(0, n + pos + 1) if pos < 0 else min(pos, n)
370+
dims.insert(insert_pos, "variable")
371371
da = da.transpose(*dims)
372372
return da
373373
return self._ds[var]

0 commit comments

Comments
 (0)