|
7 | 7 |
|
8 | 8 | from xarray_plotly import plotting |
9 | 9 | from xarray_plotly.common import SlotValue, auto |
| 10 | +from xarray_plotly.config import _options |
10 | 11 |
|
11 | 12 |
|
12 | 13 | class DataArrayPlotlyAccessor: |
@@ -353,21 +354,20 @@ def _get_dataarray(self, var: str | None) -> DataArray: |
353 | 354 |
|
354 | 355 | When combining all variables, "variable" is placed at the position |
355 | 356 | specified by config.dataset_variable_position (default 1, second position). |
| 357 | + Supports Python-style negative indexing: -1 = last, -2 = second-to-last, etc. |
356 | 358 | """ |
357 | 359 | if var is None: |
358 | | - from xarray_plotly.config import _options |
359 | | - |
360 | 360 | da = self._ds.to_array(dim="variable") |
361 | 361 | pos = _options.dataset_variable_position |
362 | 362 | # Move "variable" to configured position |
363 | 363 | if len(da.dims) > 1 and pos != 0: |
364 | 364 | dims = list(da.dims) |
365 | 365 | 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") |
371 | 371 | da = da.transpose(*dims) |
372 | 372 | return da |
373 | 373 | return self._ds[var] |
|
0 commit comments