Skip to content

Fix Stack distorting bar heights with a non-zero baseline (#4014) - #4039

Open
themythofsisyphusjayant wants to merge 1 commit into
mwaskom:masterfrom
themythofsisyphusjayant:fix-4014-stack-nonzero-baseline
Open

themythofsisyphusjayant wants to merge 1 commit into
mwaskom:masterfrom
themythofsisyphusjayant:fix-4014-stack-nonzero-baseline

Conversation

@themythofsisyphusjayant

Copy link
Copy Markdown

The bug

In the seaborn.objects interface, so.Stack is meant to stack bar/area marks cumulatively along the value axis. With a non-zero baseline (e.g. so.Bar(baseline=10)), the first bar at each position got a height of 0 (or negative) and was dropped or drawn upside-down, and every later bar was shifted down by the baseline:

df = pd.DataFrame({"x": ["A", "A"], "y": [15.0, 25.0], "group": ["g1", "g2"]})
so.Plot(df, x="x", y="y", color="group").add(so.Bar(baseline=10), so.Stack())
# Before: patches (y=10, height=-5) and (y=15, height=5)
#         — the first bar is inverted; on newer matplotlib it is dropped
# After:  patches (y=10, height=5) and (y=15, height=15) as expected

Root cause

In Stack._stack (seaborn/_core/moves.py), stacked_lengths holds the cumulative lengths measured from the baseline, but it was assigned directly to the value column as if it were the absolute coordinate:

df[other] = stacked_lengths

That is only correct when the baseline is 0. With a non-zero baseline the absolute top of each bar is baseline + stacked_lengths.

The fix

One line in Stack._stack:

df[other] = df["baseline"] + stacked_lengths

The baseline column itself is unchanged by this (df["baseline"] is still baseline + offsets), and the per-group baseline is constant within a group (_stack already raises when baselines are heterogeneous), so the addition is well-defined. Missing values behave as before: rows dropped from stacked_lengths by .dropna() stay NaN through the index-aligned addition, exactly as with the old assignment.

Tests

New tests in tests/_core/test_moves.py::TestStack:

  • test_nonzero_baseline — the issue's case: y [15, 30], baseline [10, 15].
  • test_nonzero_baseline_orient_y — same stacking, horizontal orientation (value on x).
  • test_nonzero_baseline_missing_data — a missing value in the middle keeps the baseline offset for the surviving rows.

Results (pandas 2.1.4 on the test machine is older than seaborn master expects, so some unrelated tests fail identically with and without this change — see the pristine controls):

  • New tests alone: 3 passed; with the fix stashed: 3 failed.
  • tests/_core/test_moves.py: 37 passed with the fix; 34 passed / 3 failed (exactly the new tests) without it.
  • tests/_core/ (whole directory): 573 passed / 8 failed with the fix; 570 passed / 11 failed without it — the failure-set diff is exactly the 3 new tests; the other 8 failures are identical pre-existing ones.
  • tests/_marks/test_bar.py: 16 passed with the fix, 16 passed without.
  • ruff check on both changed files: clean. (ruff format flags both files, but every flagged hunk is pre-existing code; no reformatting was done, to keep the diff minimal.)

Notes

I used an AI coding assistant (Muse Spark) to help prepare this change, and reviewed the diff myself before submitting.

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.

Move transforms do not respect requested scale order

1 participant