Repository navigation
Fix Stack distorting bar heights with a non-zero baseline (#4014) - #4039
Open
themythofsisyphusjayant wants to merge 1 commit into
Open
themythofsisyphusjayant wants to merge 1 commit into
themythofsisyphusjayant wants to merge 1 commit into
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The bug
In the
seaborn.objectsinterface,so.Stackis meant to stack bar/area marks cumulatively along the value axis. With a non-zerobaseline(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:Root cause
In
Stack._stack(seaborn/_core/moves.py),stacked_lengthsholds the cumulative lengths measured from the baseline, but it was assigned directly to the value column as if it were the absolute coordinate: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:The baseline column itself is unchanged by this (
df["baseline"]is stillbaseline + offsets), and the per-group baseline is constant within a group (_stackalready raises when baselines are heterogeneous), so the addition is well-defined. Missing values behave as before: rows dropped fromstacked_lengthsby.dropna()stayNaNthrough 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):
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 checkon both changed files: clean. (ruff formatflags both files, but every flagged hunk is pre-existing code; no reformatting was done, to keep the diff minimal.)Notes
Stack.__call__, row reordering before the groupby apply); it does not touch_stack, so this change does not conflict with it.I used an AI coding assistant (Muse Spark) to help prepare this change, and reviewed the diff myself before submitting.