Commit 4a492f4
Remove fxplot accessor in favor of external ploty accessor (#560)
* Remove fxplot accessor in favor of external ploty accessor
* Moved stats accessor to new module name
* Reuse fxstats.duration_curve
* Update notebooks
* Update notebooks
* Remove auto
* Set x='time' for most plots
* Remove slot params
* Improve color handling
* 1. statistics_accessor.py
- Removed explicit slot parameters (facet_col, facet_row, animation_frame) from all plotting methods
- Added _build_color_kwargs() helper to consistently handle dict, list, and string colors
- Refactored all methods to use _build_color_kwargs for consistent color handling
- Updated _prepare_for_heatmap() to support reshape='auto' (default)
- Slot parameters are now passed through via **plotly_kwargs
2. clustering/base.py
- Removed explicit slot parameters from compare() and heatmap() methods
- Parameters passed through via **plotly_kwargs
3. comparison.py
- Completely refactored ComparisonStatisticsPlot:
- Removed the complex generic _plot() method
- Each method now has explicit parameters with proper docstrings
- Uses _build_color_kwargs consistently
- Changed ds.fxplot to ds.plotly
- Changed stacked_bar to bar with barmode='relative'
Key Design Patterns
1. Color handling: All methods use _build_color_kwargs(colors, labels) which handles:
- dict → color_discrete_map
- list → color_discrete_sequence
- str (colorscale name) → converted to color_discrete_map via process_colors()
2. Dimension slots: Let xarray-plotly handle assignments by default, only set explicitly when needed (e.g., x='time' for time series)
3. Consistent API: All methods now have explicit parameters instead of **kwargs everywhere
* ⏺ All done! Here's a summary of the changes:
Changes to flixopt/comparison.py:
1. Added import: from xarray_plotly import SLOT_ORDERS
2. Added helper function:
_CASE_SLOTS = frozenset(slot for slots in SLOT_ORDERS.values() for slot in slots)
def _set_case_default(kwargs: dict, slot: str) -> None:
"""Set default slot for 'case' dimension if not already assigned elsewhere."""
if not any(kwargs.get(s) == 'case' for s in _CASE_SLOTS):
kwargs.setdefault(slot, 'case')
3. Updated all 9 plotting methods with sensible defaults:
| Method | Default Slot |
|-------------------|--------------------------------|
| balance() | facet_col='case' |
| carrier_balance() | facet_col='case' |
| storage() | facet_col='case' |
| flows() | line_dash='case' |
| charge_states() | line_dash='case' |
| duration_curve() | line_dash='case' |
| sizes() | color='case' + barmode='group' |
| effects() | color='case' + barmode='group' |
| heatmap() | facet_col='case' |
The defaults are "safe" - if the user explicitly assigns 'case' to any slot, the default won't override it.
* ⏺ Add sensible slot defaults for comparison plots and block unused slots
Comparison plots now automatically assign the 'case' dimension to
appropriate slots based on plot type:
- Stacked bars (balance, carrier_balance, storage): facet_col='case'
- Line charts (flows, charge_states, duration_curve): line_dash='case'
- Grouped bars (sizes, effects): color='case' with barmode='group'
- Heatmaps: facet_col='case'
Uses _set_case_default() helper that checks if 'case' is already
assigned elsewhere before setting the default, preventing conflicts.
Also blocks unused slots in StatisticsPlotAccessor to prevent
xarray_plotly from auto-assigning dimensions to visually unhelpful
slots:
- Time-series bars: pattern_shape=None
- Line charts: symbol=None
All defaults use setdefault() so users can override when needed.
* Add sensible slot defaults for plotting methods
Use setdefault() for all dimension-to-slot assignments, allowing user
overrides while providing sensible defaults.
Comparison plots (ComparisonStatisticsPlot):
- Stacked bars: x='time', facet_col='case'
- Line charts: x='time', line_dash='case'
- Grouped bars: x='variable', color='case', barmode='group'
- Duration curve: x='duration_pct'/'duration'
- Heatmaps: facet_col='case'
Statistics plots (StatisticsPlotAccessor):
- Time-series bars: x='time', pattern_shape=None (blocked)
- Line charts: x='time', symbol=None (blocked)
- Duration curve: x='duration_pct'/'duration'
- Sizes: x='variable'
- Effects: x computed from 'by' parameter
Uses SLOT_ORDERS from xarray_plotly to dynamically detect all valid
slots when checking for 'case' dimension conflicts.
All defaults use setdefault() so users can override when needed.
* Updated _SLOT_DEFAULTS dict with proper plotly kwargs (no custom keys):
_SLOT_DEFAULTS: dict[str, dict[str, str | None]] = {
'balance': {'x': 'time', 'color': 'variable', 'pattern_shape': None, 'facet_col': 'case'},
'carrier_balance': {'x': 'time', 'color': 'variable', 'pattern_shape': None, 'facet_col': 'case'},
'flows': {'x': 'time', 'color': 'variable', 'symbol': None, 'line_dash': 'case'},
'charge_states': {'x': 'time', 'color': 'variable', 'symbol': None, 'line_dash': 'case'},
'storage': {'x': 'time', 'color': 'variable', 'pattern_shape': None, 'facet_col': 'case'},
'sizes': {'x': 'variable', 'color': 'case'},
'duration_curve': {'symbol': None, 'line_dash': 'case'},
'effects': {'color': 'case'},
'heatmap': {'facet_col': 'case'},
}
Updated _apply_slot_defaults() helper to detect if 'case' is already assigned:
- If user has already set 'case' to any slot, skips the default case assignment
- Otherwise applies all defaults via setdefault() (user overridable)
Updated all 9 methods (balance, carrier_balance, flows, charge_states, storage, duration_curve, sizes, effects, heatmap) to use:
_apply_slot_defaults(plotly_kwargs, 'method_name')
* Move slot defaults inline into each comparison plot method
Improves code locality by defining defaults dict directly in each method
rather than in a module-level lookup table.
* Build job (PRs & pushes):
- Only runs fast notebooks (~1 min total)
- Slow notebooks are skipped
Deploy job (releases only):
- Runs fast notebooks first
- Then runs slow notebooks (~10 min extra)
- Full documentation with all notebooks executed
Files changed:
- .github/workflows/docs.yaml - updated both execution steps
- docs/notebooks/slow_notebooks.txt - created with the 2 slow notebooks
To add/remove slow notebooks in the future, just edit slow_notebooks.txt.
* Fix storage plot passing invalid kwargs to add_line_overlay
Filter plotly_kwargs to only pass faceting-related kwargs (x, facet_col,
facet_row, animation_frame) to add_line_overlay, avoiding TypeError from
bar-chart-specific kwargs like pattern_shape.
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
* Fix storage plot passing invalid kwargs to add_line_overlay
Filter plotly_kwargs to only pass faceting-related kwargs (x, facet_col,
facet_row, animation_frame) to add_line_overlay, avoiding TypeError from
bar-chart-specific kwargs like pattern_shape.
Applied to both statistics_accessor.py and comparison.py.
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
* 1. Identified slow notebooks - 08b-rolling-horizon (5 min) and 08c2-clustering-storage-modes (5 min)
2. Created slow_notebooks.txt - config file to exclude them from regular CI
3. Updated CI - fast notebooks run on every build, slow ones only on release
4. Switched to Plotly CDN - notebooks now ~98% smaller (4.8MB → 79KB each)
* - Type: tuple[str, str] | Literal['auto'] | None
- Docstring: "Use 'auto' to reshape to ('D', 'h') if data has a time index"
* Apply slot defaults pattern to clustering and fix color handling
- Add _apply_slot_defaults helper for clustering plots
- Use inline defaults dict in compare() and clusters() methods
- Remove explicit slot parameters (color, line_dash, facet_col) from signatures
- Pass all slot assignments through **plotly_kwargs with sensible defaults
- Fix color handling: use _build_color_kwargs for proper ColorType support
- Apply default colorscale for heatmap when colors=None
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
* Add other plot to notebook
* Add plotly template
* Add plotly template
* Fix notebook
* Add docs about ploty express
* More about plotly templates
* Fixed. Now n is calculated explicitly from the first entry in sorted_vars rather than relying on the implicit loop variable from the last iteration.
* Template System
- 'flixopt' template registered on import (no side effects)
- CONFIG.use_theme() to explicitly activate
- Fixed .title() for colorscale names + fallback handling
Color Handling Simplification
- process_colors() reads CONFIG internally
- Removed 8+ explicit default_colorscale=CONFIG... calls
- Heatmaps use template default when colors not specified
Heatmap Default
- Changed reshape=None → reshape=('D', 'h')
- More useful out-of-the-box daily pattern visualization
Documentation
- Added "Plotly Express Internals" section covering slots, colorscales, templates
- Fixed markdown code block language specifiers
* All fixes have been applied and verified. Here's a summary of the changes:
Changes Made
1. CHANGELOG.md (line 145)
- Before: #### FXPlot Accessor (#548)
- After: #### Plotly Accessor (#548)
2. CHANGELOG.md (lines 169-170)
- Before: Two duplicate .plotly.bar() entries (grouped/stacked)
- After: Single entry: .plotly.bar() | Bar charts (use barmode='group' or 'relative' for stacked)
3. flixopt/stats_accessor.py - to_duration_curve() (lines 43-66)
- Before: Used da.values (breaks dask laziness) and tuple assignment (loses attrs)
- After: Uses xr.apply_ufunc() with dask='parallelized' to preserve:
- Dask lazy evaluation
- DataArray attributes
- Dataset attributes
- Variables without time dimension (unchanged)
4. docs/user-guide/recipes/plotting-custom-data.md
- Added note explaining that .plotly and .fxstats accessors are automatically registered on import
* 1. flixopt/stats_accessor.py (line 67-69) - Preserve non-time coordinates
# Before
sorted_ds = xr.Dataset(sorted_vars, attrs=self._ds.attrs)
# After
non_time_coords = {k: v for k, v in self._ds.coords.items() if k != 'time'}
sorted_ds = xr.Dataset(sorted_vars, coords=non_time_coords, attrs=self._ds.attrs)
2. flixopt/config.py (line 820-821) - Re-register template on use_theme()
# Re-register template to pick up any config changes made after import
_register_flixopt_template()
pio.templates.default = 'plotly_white+flixopt'
Now if a user modifies CONFIG.Plotting.default_qualitative_colorscale after import but before calling use_theme(), the template will use the updated value.
3. flixopt/config.py (line 958-963) - Log warning on colorscale fallback
if colorway is None:
logging.getLogger(__name__).warning(
f"Colorscale '{CONFIG.Plotting.default_qualitative_colorscale}' not found in "
f"plotly.express.colors.qualitative, falling back to 'Plotly'. "
f"Available: {[n for n in dir(colors.qualitative) if not n.startswith('_')]}"
)
colorway = colors.qualitative.Plotly
* ⏺ Much cleaner. The simplified implementation:
- Preserves DataArray attrs via attrs=da.attrs
- Preserves variable-level coords via coords={k: v for k, v in da.coords.items() if k != 'time'}
- Preserves dataset-level coords and attrs separately
---------
Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>1 parent 7e3a118 commit 4a492f4
22 files changed
Lines changed: 1169 additions & 1989 deletions
File tree
- docs
- notebooks
- user-guide
- recipes
- flixopt
- clustering
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
53 | 53 | | |
54 | 54 | | |
55 | 55 | | |
56 | | - | |
| 56 | + | |
57 | 57 | | |
58 | 58 | | |
59 | 59 | | |
60 | 60 | | |
61 | 61 | | |
62 | 62 | | |
63 | 63 | | |
64 | | - | |
| 64 | + | |
65 | 65 | | |
66 | 66 | | |
67 | 67 | | |
| |||
142 | 142 | | |
143 | 143 | | |
144 | 144 | | |
145 | | - | |
| 145 | + | |
146 | 146 | | |
147 | 147 | | |
148 | 148 | | |
149 | 149 | | |
150 | 150 | | |
151 | 151 | | |
152 | 152 | | |
153 | | - | |
154 | | - | |
155 | | - | |
156 | | - | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
157 | 157 | | |
158 | 158 | | |
159 | | - | |
| 159 | + | |
160 | 160 | | |
161 | 161 | | |
162 | 162 | | |
| |||
166 | 166 | | |
167 | 167 | | |
168 | 168 | | |
169 | | - | |
170 | | - | |
171 | | - | |
172 | | - | |
173 | | - | |
174 | | - | |
175 | | - | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
176 | 175 | | |
177 | 176 | | |
178 | 177 | | |
| |||
195 | 194 | | |
196 | 195 | | |
197 | 196 | | |
198 | | - | |
| 197 | + | |
199 | 198 | | |
200 | 199 | | |
201 | 200 | | |
| |||
310 | 309 | | |
311 | 310 | | |
312 | 311 | | |
313 | | - | |
314 | 312 | | |
315 | 313 | | |
316 | 314 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
72 | 72 | | |
73 | 73 | | |
74 | 74 | | |
75 | | - | |
| 75 | + | |
76 | 76 | | |
77 | 77 | | |
78 | 78 | | |
79 | 79 | | |
80 | 80 | | |
81 | | - | |
| 81 | + | |
82 | 82 | | |
83 | 83 | | |
84 | 84 | | |
| |||
98 | 98 | | |
99 | 99 | | |
100 | 100 | | |
101 | | - | |
| 101 | + | |
102 | 102 | | |
103 | 103 | | |
104 | 104 | | |
105 | 105 | | |
106 | 106 | | |
107 | | - | |
| 107 | + | |
108 | 108 | | |
109 | 109 | | |
110 | 110 | | |
| |||
375 | 375 | | |
376 | 376 | | |
377 | 377 | | |
378 | | - | |
| 378 | + | |
| 379 | + | |
| 380 | + | |
| 381 | + | |
| 382 | + | |
| 383 | + | |
| 384 | + | |
379 | 385 | | |
380 | 386 | | |
381 | 387 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
100 | 100 | | |
101 | 101 | | |
102 | 102 | | |
103 | | - | |
| 103 | + | |
104 | 104 | | |
105 | 105 | | |
106 | 106 | | |
107 | 107 | | |
108 | 108 | | |
109 | 109 | | |
110 | | - | |
| 110 | + | |
111 | 111 | | |
112 | 112 | | |
113 | 113 | | |
| |||
424 | 424 | | |
425 | 425 | | |
426 | 426 | | |
427 | | - | |
| 427 | + | |
| 428 | + | |
| 429 | + | |
| 430 | + | |
| 431 | + | |
| 432 | + | |
| 433 | + | |
428 | 434 | | |
429 | 435 | | |
430 | 436 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
85 | 85 | | |
86 | 86 | | |
87 | 87 | | |
88 | | - | |
| 88 | + | |
89 | 89 | | |
90 | 90 | | |
91 | 91 | | |
92 | 92 | | |
93 | 93 | | |
94 | | - | |
| 94 | + | |
95 | 95 | | |
96 | 96 | | |
97 | 97 | | |
| |||
250 | 250 | | |
251 | 251 | | |
252 | 252 | | |
253 | | - | |
| 253 | + | |
254 | 254 | | |
255 | 255 | | |
256 | 256 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
101 | 101 | | |
102 | 102 | | |
103 | 103 | | |
104 | | - | |
| 104 | + | |
105 | 105 | | |
106 | 106 | | |
107 | 107 | | |
108 | 108 | | |
109 | 109 | | |
110 | 110 | | |
111 | 111 | | |
112 | | - | |
| 112 | + | |
113 | 113 | | |
114 | 114 | | |
115 | 115 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
93 | 93 | | |
94 | 94 | | |
95 | 95 | | |
96 | | - | |
| 96 | + | |
97 | 97 | | |
98 | 98 | | |
99 | 99 | | |
100 | 100 | | |
101 | 101 | | |
102 | 102 | | |
103 | | - | |
| 103 | + | |
104 | 104 | | |
105 | 105 | | |
106 | 106 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
100 | 100 | | |
101 | 101 | | |
102 | 102 | | |
103 | | - | |
| 103 | + | |
104 | 104 | | |
105 | 105 | | |
106 | 106 | | |
| |||
111 | 111 | | |
112 | 112 | | |
113 | 113 | | |
114 | | - | |
| 114 | + | |
115 | 115 | | |
116 | 116 | | |
117 | 117 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
77 | 77 | | |
78 | 78 | | |
79 | 79 | | |
80 | | - | |
| 80 | + | |
81 | 81 | | |
82 | 82 | | |
83 | 83 | | |
| |||
461 | 461 | | |
462 | 462 | | |
463 | 463 | | |
464 | | - | |
| 464 | + | |
465 | 465 | | |
466 | 466 | | |
467 | 467 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
120 | 120 | | |
121 | 121 | | |
122 | 122 | | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
123 | 144 | | |
124 | 145 | | |
125 | 146 | | |
| |||
128 | 149 | | |
129 | 150 | | |
130 | 151 | | |
131 | | - | |
| 152 | + | |
132 | 153 | | |
133 | 154 | | |
134 | 155 | | |
| |||
139 | 160 | | |
140 | 161 | | |
141 | 162 | | |
142 | | - | |
| 163 | + | |
143 | 164 | | |
144 | 165 | | |
145 | 166 | | |
146 | 167 | | |
147 | 168 | | |
148 | | - | |
| 169 | + | |
149 | 170 | | |
150 | 171 | | |
151 | 172 | | |
152 | 173 | | |
153 | 174 | | |
154 | | - | |
| 175 | + | |
155 | 176 | | |
156 | 177 | | |
157 | 178 | | |
| |||
161 | 182 | | |
162 | 183 | | |
163 | 184 | | |
164 | | - | |
| 185 | + | |
165 | 186 | | |
166 | 187 | | |
167 | 188 | | |
| |||
173 | 194 | | |
174 | 195 | | |
175 | 196 | | |
176 | | - | |
| 197 | + | |
177 | 198 | | |
178 | 199 | | |
179 | 200 | | |
| |||
192 | 213 | | |
193 | 214 | | |
194 | 215 | | |
195 | | - | |
| 216 | + | |
196 | 217 | | |
197 | 218 | | |
198 | 219 | | |
| |||
236 | 257 | | |
237 | 258 | | |
238 | 259 | | |
239 | | - | |
| 260 | + | |
240 | 261 | | |
241 | 262 | | |
242 | 263 | | |
| |||
252 | 273 | | |
253 | 274 | | |
254 | 275 | | |
255 | | - | |
| 276 | + | |
256 | 277 | | |
257 | 278 | | |
258 | 279 | | |
| |||
262 | 283 | | |
263 | 284 | | |
264 | 285 | | |
265 | | - | |
| 286 | + | |
266 | 287 | | |
267 | 288 | | |
268 | 289 | | |
| |||
273 | 294 | | |
274 | 295 | | |
275 | 296 | | |
276 | | - | |
| 297 | + | |
277 | 298 | | |
278 | 299 | | |
279 | 300 | | |
| |||
287 | 308 | | |
288 | 309 | | |
289 | 310 | | |
290 | | - | |
| 311 | + | |
| 312 | + | |
| 313 | + | |
| 314 | + | |
| 315 | + | |
| 316 | + | |
| 317 | + | |
291 | 318 | | |
292 | 319 | | |
293 | 320 | | |
0 commit comments