feat: add fast_bar() method for high-performance bar-like charts#17
feat: add fast_bar() method for high-performance bar-like charts#17
Conversation
Summary of changes: 1. plotting.py: Added barlike parameter to area() with helper _apply_barlike_style() 2. accessor.py: Updated both DataArrayPlotlyAccessor.area() and DatasetPlotlyAccessor.area() 3. Tests: Added 3 tests for barlike (basic, trace styling, animation frames) 4. Notebook: Created docs/examples/barlike.ipynb demonstrating the feature Usage: xpx(da).area(barlike=True) # Looks like bars, renders faster
Summary of changes: 1. plotting.py: Added barlike parameter to area() with helper _apply_barlike_style() 2. accessor.py: Updated both DataArrayPlotlyAccessor.area() and DatasetPlotlyAccessor.area() 3. Tests: Added 3 tests for barlike (basic, trace styling, animation frames) 4. Notebook: Created docs/examples/barlike.ipynb demonstrating the feature Usage: xpx(da).area(barlike=True) # Looks like bars, renders faster
┌──────────────┬─────────────────────────────────────┐ │ Data │ Behavior │ ├──────────────┼─────────────────────────────────────┤ │ All positive │ Stacked (stackgroup=1) │ ├──────────────┼─────────────────────────────────────┤ │ All negative │ Stacked (stackgroup=1) │ ├──────────────┼─────────────────────────────────────┤ │ Mixed +/- │ No stacking, fill to zero (tozeroy) │ └──────────────┴─────────────────────────────────────┘ Changes made: - plotting.py: Added _has_mixed_signs() detection, updated _apply_barlike_style() to handle mixed signs - test_accessor.py: Added 2 new tests for mixed/same-sign behavior - fast_bar.ipynb: Updated with negative and mixed value examples, documented the behavior Note: For mixed data, series will overlap since stacking doesn't work well with mixed signs. The notebook documents that bar() should be used if proper stacking of mixed data is needed.
fast_bar() now properly handles mixed positive/negative data:
- Same-sign data (all positive or all negative): Uses single stackgroup for normal stacking
- Mixed-sign data: Splits each trace into positive and negative parts with separate stackgroups
- Positives stack upward from zero (stackgroup='positive')
- Negatives stack downward from zero (stackgroup='negative')
- This matches bar chart behavior with barmode='relative'
Implementation:
- Added _split_traces_by_sign() helper that creates separate traces for positive and negative values
- Updated fast_bar() to detect mixed signs and apply the split
- Animation frames are also handled correctly
Summary: ┌─────────────────────┬─────────────────────────────────────────┐ │ Trace type │ Behavior │ ├─────────────────────┼─────────────────────────────────────────┤ │ All positive values │ stackgroup='positive' - stacks upward │ ├─────────────────────┼─────────────────────────────────────────┤ │ All negative values │ stackgroup='negative' - stacks downward │ ├─────────────────────┼─────────────────────────────────────────┤ │ Mixed +/- values │ stackgroup=None, dashed line, no fill │ └─────────────────────┴─────────────────────────────────────────┘ Code is simpler: - _style_traces_as_bars() - single function that classifies and styles all traces - No trace splitting needed - Works correctly for facets and animations Notebook updated with examples showing: 1. Split columns (Profit positive, Loss negative) → proper stacking 2. Truly mixed columns → dashed lines indicating user should use bar()
|
Caution Review failedThe pull request is closed. 📝 WalkthroughWalkthroughA new fast_bar visualization is added: plotting.fast_bar implements area-based bar rendering with trace sign classification and styling; DataArrayPlotlyAccessor and DatasetPlotlyAccessor gain .fast_bar methods; DEFAULT_SLOT_ORDERS updated; tests and a docs example notebook plus mkdocs nav entries were added. Changes
Sequence DiagramsequenceDiagram
participant User
participant Accessor as DataArrayPlotly<br/>Accessor
participant Plotting as plotting<br/>module
participant Express as plotly.express
participant Figure as go.Figure
User->>Accessor: .fast_bar(x, color, facet_col, animation_frame)
Accessor->>Plotting: fast_bar(darray, x=..., color=..., ...)
Plotting->>Express: area(..., x=..., color=..., facet_col=..., animation_frame=...)
Express-->>Plotting: Figure with area traces
Plotting->>Plotting: _style_traces_as_bars(fig)
rect rgba(100,150,200,0.5)
Note over Plotting: classify trace signs, assign stackgroups,\napply fill/stroke/dash styling, warn on mixed-signs
end
Plotting-->>Accessor: styled go.Figure
Accessor-->>User: go.Figure
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~25 minutes Possibly related PRs
Poem
🚥 Pre-merge checks | ✅ 3✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing touches
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Fix all issues with AI agents
In `@xarray_plotly/accessor.py`:
- Around line 163-196: The fast_bar accessor method is not exported via __all__,
so it won't show up in tab-completion; update the module-level __all__ to
include "fast_bar" (and add it to the __all__ list for both accessor exports if
there are two accessor modules/objects) so that __dir__ can discover it; locate
the fast_bar definition in accessor.py and ensure the string "fast_bar" is
present in the module's __all__ sequence alongside the other accessor names.
🧹 Nitpick comments (1)
xarray_plotly/plotting.py (1)
171-185: Consider reusing_classify_trace_signto avoid duplicate logic.
Right now classification is re-implemented in_style_traces_as_bars; either use the helper or remove it to keep one source of truth.
Summary
Adds
fast_bar()method that creates bar-like visualizations using stacked areas. Renders significantly faster thanbar()for large datasets because it uses a single polygon per trace instead of individual rectangles.Features
Stepped area rendering - Uses
px.areawithline_shape='hv'and no outline for bar-like appearanceSmart sign handling - Classifies traces and assigns appropriate stackgroups:
'positive''negative'NoneFaceting & animation support - Works with
facet_col,facet_row,animation_frameUser warning - Warns when traces have mixed signs and recommends using
bar()API
When to use
fast_bar()bar()Changes
plotting.py: Addedfast_bar()function and_style_traces_as_bars()helperconfig.py: Addedfast_barslot orderaccessor.py: Addedfast_bar()to bothDataArrayPlotlyAccessorandDatasetPlotlyAccessortest_accessor.py: Added 6 tests for fast_bar behaviordocs/examples/fast_bar.ipynb: Comprehensive examples with faceting, animation, and sign handlingTest plan
Summary by CodeRabbit
New Features
Documentation
✏️ Tip: You can customize this high-level summary in your review settings.