Skip to content

Commit 3de8bf8

Browse files
committed
Added configurable dataset_variable_position to dataset plots
1 parent 79a7e8c commit 3de8bf8

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed

xarray_plotly/accessor.py

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -349,9 +349,27 @@ def __dir__(self) -> list[str]:
349349
return list(self.__all__) + list(super().__dir__())
350350

351351
def _get_dataarray(self, var: str | None) -> DataArray:
352-
"""Get DataArray from Dataset, either single var or all via to_array()."""
352+
"""Get DataArray from Dataset, either single var or all via to_array().
353+
354+
When combining all variables, "variable" is placed at the position
355+
specified by config.dataset_variable_position (default 1, second position).
356+
"""
353357
if var is None:
354-
return self._ds.to_array(dim="variable")
358+
from xarray_plotly.config import _options
359+
360+
da = self._ds.to_array(dim="variable")
361+
pos = _options.dataset_variable_position
362+
# Move "variable" to configured position
363+
if len(da.dims) > 1 and pos != 0:
364+
dims = list(da.dims)
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")
371+
da = da.transpose(*dims)
372+
return da
355373
return self._ds[var]
356374

357375
def line(

xarray_plotly/config.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@ class Options:
5858
label_include_units: Append units to labels. Default True.
5959
label_unit_format: Format string for units. Use `{units}` as placeholder.
6060
slot_orders: Slot orders per plot type. Keys are plot types, values are tuples.
61+
dataset_variable_position: Position of "variable" dim when plotting all Dataset
62+
variables. Default 1 (second position, typically color). Set to 0 for first
63+
position (x-axis), or -1 for last position.
6164
"""
6265

6366
label_use_long_name: bool = True
@@ -67,6 +70,7 @@ class Options:
6770
slot_orders: dict[str, tuple[str, ...]] = field(
6871
default_factory=lambda: dict(DEFAULT_SLOT_ORDERS)
6972
)
73+
dataset_variable_position: int = 1
7074

7175
def to_dict(self) -> dict[str, Any]:
7276
"""Return options as a dictionary."""
@@ -76,6 +80,7 @@ def to_dict(self) -> dict[str, Any]:
7680
"label_include_units": self.label_include_units,
7781
"label_unit_format": self.label_unit_format,
7882
"slot_orders": self.slot_orders,
83+
"dataset_variable_position": self.dataset_variable_position,
7984
}
8085

8186

@@ -106,6 +111,7 @@ def set_options(
106111
label_include_units: bool | None = None,
107112
label_unit_format: str | None = None,
108113
slot_orders: dict[str, tuple[str, ...]] | None = None,
114+
dataset_variable_position: int | None = None,
109115
) -> Generator[None, None, None]:
110116
"""Set xarray_plotly options globally or as a context manager.
111117
@@ -115,6 +121,8 @@ def set_options(
115121
label_include_units: Append units to labels.
116122
label_unit_format: Format string for units. Use `{units}` as placeholder.
117123
slot_orders: Slot orders per plot type.
124+
dataset_variable_position: Position of "variable" dim when plotting all Dataset
125+
variables. Default 1 (second, typically color). Use 0 for first, -1 for last.
118126
119127
Yields:
120128
None when used as a context manager.
@@ -136,6 +144,7 @@ def set_options(
136144
"label_include_units": _options.label_include_units,
137145
"label_unit_format": _options.label_unit_format,
138146
"slot_orders": dict(_options.slot_orders),
147+
"dataset_variable_position": _options.dataset_variable_position,
139148
}
140149

141150
# Apply new values (modify in place to keep reference)
@@ -149,6 +158,8 @@ def set_options(
149158
_options.label_unit_format = label_unit_format
150159
if slot_orders is not None:
151160
_options.slot_orders = dict(slot_orders)
161+
if dataset_variable_position is not None:
162+
_options.dataset_variable_position = dataset_variable_position
152163

153164
try:
154165
yield
@@ -159,6 +170,7 @@ def set_options(
159170
_options.label_include_units = old_values["label_include_units"]
160171
_options.label_unit_format = old_values["label_unit_format"]
161172
_options.slot_orders = old_values["slot_orders"]
173+
_options.dataset_variable_position = old_values["dataset_variable_position"]
162174

163175

164176
def notebook(renderer: str = "notebook") -> None:

0 commit comments

Comments
 (0)