Skip to content

Added robust parameter to imshow() with global bounds computation#13

Merged
FBumann merged 1 commit intomainfrom
feature/imshow-bounds
Jan 10, 2026
Merged

Added robust parameter to imshow() with global bounds computation#13
FBumann merged 1 commit intomainfrom
feature/imshow-bounds

Conversation

@FBumann
Copy link
Owner

@FBumann FBumann commented Jan 10, 2026

…th global bounds computation

  1. xarray_plotly/accessor.py: Added robust parameter to accessor method
  2. tests/test_accessor.py: Added 4 tests for bounds behavior

New behavior:

  • Default: Global min/max across all data (fixes animation consistency)
  • robust=True: Uses 2nd/98th percentile (handles outliers)
  • zmin/zmax: User override still works

Summary by CodeRabbit

Release Notes

  • New Features

    • Added robust parameter to imshow() for percentile-based color scaling that handles data outliers more gracefully
    • Manual color bound control via zmin and zmax parameters remains supported and takes precedence
  • Tests

    • Added comprehensive test suite validating color bounds behavior across multiple scenarios including animation frames

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

…h global bounds computation

  2. xarray_plotly/accessor.py: Added robust parameter to accessor method
  3. tests/test_accessor.py: Added 4 tests for bounds behavior

  New behavior:
  - Default: Global min/max across all data (fixes animation consistency)
  - robust=True: Uses 2nd/98th percentile (handles outliers)
  - zmin/zmax: User override still works
@coderabbitai
Copy link

coderabbitai bot commented Jan 10, 2026

📝 Walkthrough

Walkthrough

This PR adds a robust parameter to the imshow functionality that computes global color bounds using percentile-based scaling (2nd to 98th percentile) when enabled, or min/max scaling when disabled. Four new tests validate bounds computation across default, robust, user-override, and animation scenarios.

Changes

Cohort / File(s) Summary
Test Suite
tests/test_accessor.py
Adds TestImshowBounds with four tests validating imshow color bounds: default global min/max, robust percentile-based bounds, user zmin/zmax override precedence, and animation frame consistency.
Accessor API
xarray_plotly/accessor.py
Adds robust: bool = False parameter to DataArrayPlotlyAccessor.imshow() method signature and threads it to underlying plotting call; updates docstring.
Core Plotting Logic
xarray_plotly/plotting.py
Adds numpy import and robust: bool = False parameter to imshow() function. Computes global color bounds (zmin/zmax) using 2nd–98th percentiles when robust=True or min/max when robust=False, inserting them into px_kwargs as defaults when absent.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~12 minutes

Poem

🐰 Percentiles dance from 2nd to 98th with glee,
Robust bounds keep outliers out—colors stay fancy-free!
Through accessor and plotting they thread their way,
Each frame holds steady, come what may,
Heatmaps now scale with grace! ✨

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately summarizes the main changes: adding a 'robust' parameter to imshow() and implementing global bounds computation.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.

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

✨ Finishing touches
  • 📝 Generate docstrings

📜 Recent review details

Configuration used: defaults

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between afcc9b9 and b248ae2.

📒 Files selected for processing (3)
  • tests/test_accessor.py
  • xarray_plotly/accessor.py
  • xarray_plotly/plotting.py
🧰 Additional context used
🧬 Code graph analysis (1)
tests/test_accessor.py (2)
xarray_plotly/accessor.py (1)
  • imshow (246-280)
xarray_plotly/plotting.py (1)
  • imshow (395-468)
🔇 Additional comments (5)
tests/test_accessor.py (1)

297-343: Excellent test coverage for the new robust bounds feature.

The test suite comprehensively validates:

  • Global bounds computation (default behavior)
  • Percentile-based bounds with robust=True
  • User override behavior with explicit zmin/zmax
  • Consistent bounds across animation frames

All test assertions are appropriate and the test data is well-chosen for each scenario.

xarray_plotly/accessor.py (1)

253-279: Clean integration of the robust parameter.

The parameter is correctly threaded through to plotting.imshow() and the docstring clearly explains the behavior and override mechanism.

xarray_plotly/plotting.py (3)

9-9: LGTM: numpy import added for bounds computation.


402-402: Well-documented parameter addition.

The robust parameter is clearly explained in the docstring, including the percentile values used and how users can manually override with zmin/zmax.

Also applies to: 423-428


449-461: Solid implementation of global bounds computation.

The logic correctly:

  • Computes global bounds when not explicitly provided
  • Uses NaN-aware numpy functions for robustness
  • Applies percentile-based scaling in robust mode
  • Preserves user-provided zmin/zmax via setdefault

The condition on line 450 computes both bounds if either is missing, which ensures consistency when mixing computed and user-provided values. While this means occasionally computing values that won't be used (e.g., computing zmin when user provided it but not zmax), this keeps the code simple and ensures both bounds are derived from the same statistical approach.


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.

@FBumann FBumann merged commit d50e2e4 into main Jan 10, 2026
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