Skip to content

feat: add fast_bar() method for high-performance bar-like charts#17

Merged
FBumann merged 8 commits intomainfrom
feature/fast-bar
Jan 22, 2026
Merged

feat: add fast_bar() method for high-performance bar-like charts#17
FBumann merged 8 commits intomainfrom
feature/fast-bar

Conversation

@FBumann
Copy link
Owner

@FBumann FBumann commented Jan 22, 2026

Summary

Adds fast_bar() method that creates bar-like visualizations using stacked areas. Renders significantly faster than bar() for large datasets because it uses a single polygon per trace instead of individual rectangles.

Features

  • Stepped area rendering - Uses px.area with line_shape='hv' and no outline for bar-like appearance

  • Smart sign handling - Classifies traces and assigns appropriate stackgroups:

    Trace values Stackgroup Visual
    All positive 'positive' Solid fill, stacks upward
    All negative 'negative' Solid fill, stacks downward
    Mixed +/- None Dashed line + warning
  • Faceting & animation support - Works with facet_col, facet_row, animation_frame

  • User warning - Warns when traces have mixed signs and recommends using bar()

API

from xarray_plotly import xpx

# Basic usage
fig = xpx(da).fast_bar()

# With faceting and animation
fig = xpx(da).fast_bar(facet_col="region", animation_frame="year")

# Positive/negative data stacks correctly
da_financials = ...  # Revenue (positive), Costs (negative)
fig = xpx(da_financials).fast_bar()  # Stacks up and down from zero

When to use

Method Use when...
fast_bar() Large datasets, animations, performance matters, same-sign per trace
bar() Need grouped bars, pattern fills, or mixed +/- values per trace

Changes

  • plotting.py: Added fast_bar() function and _style_traces_as_bars() helper
  • config.py: Added fast_bar slot order
  • accessor.py: Added fast_bar() to both DataArrayPlotlyAccessor and DatasetPlotlyAccessor
  • test_accessor.py: Added 6 tests for fast_bar behavior
  • docs/examples/fast_bar.ipynb: Comprehensive examples with faceting, animation, and sign handling

Test plan

  • 107 tests passing
  • Faceting support verified
  • Animation support verified
  • Positive/negative stacking verified
  • Mixed sign warning verified

Summary by CodeRabbit

  • New Features

    • Introduced fast_bar() visualization method for creating performance-optimized bar charts
    • Supports faceting by columns and animating across dimensions
    • Intelligently handles positive and negative values with appropriate stacking behavior
  • Documentation

    • Added comprehensive notebook examples demonstrating fast_bar() usage, including faceting, animation, and best practices

✏️ Tip: You can customize this high-level summary in your review settings.

  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()
@coderabbitai
Copy link

coderabbitai bot commented Jan 22, 2026

Caution

Review failed

The pull request is closed.

📝 Walkthrough

Walkthrough

A 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

Cohort / File(s) Summary
Documentation & Examples
docs/examples/fast_bar.ipynb, mkdocs.yml
New example notebook demonstrating fast_bar usage (basic, faceting, animation, mixed-sign behavior) and added mkdocs navigation entries for examples.
Plotting implementation
xarray_plotly/plotting.py
Added public fast_bar() plus helpers _classify_trace_sign() and _style_traces_as_bars(); uses plotly.express.area then post-processes traces for sign-aware stacking and dashed rendering for mixed-sign traces; emits warnings for mixed traces.
Accessors
xarray_plotly/accessor.py
Added fast_bar methods to DataArrayPlotlyAccessor and DatasetPlotlyAccessor (Dataset version accepts var), and exported them via __all__.
Configuration
xarray_plotly/config.py
Added DEFAULT_SLOT_ORDERS entry for "fast_bar": ("x","color","facet_col","facet_row","animation_frame").
Tests
tests/test_accessor.py
New tests verifying fast_bar returns a Figure, trace styling (line width/shape/fill), animation-frame handling, dashed styling for mixed-sign traces, stackgroup separation by sign, and stacking for same-sign traces.

Sequence Diagram

sequenceDiagram
    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
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Possibly related PRs

Poem

🐰 I hop, I sketch with speed and cheer,
Fast bars that stack both far and near,
Positives rise, negatives fall,
Mixed signs dash — I warn them all!
A nimble chart to celebrate here. 🎨

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
Title check ✅ Passed The title accurately summarizes the main change: adding a new fast_bar() method for high-performance bar-like charts, which is the primary feature introduced across accessor, plotting, config, documentation, and test files.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing touches
  • 📝 Generate docstrings

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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_sign to 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.

@FBumann FBumann changed the title Feature/fast bar feat: add fast_bar() method for high-performance bar-like charts Jan 22, 2026
@FBumann FBumann merged commit c677239 into main Jan 22, 2026
8 of 9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant