From ad7c6e26ae373f7cd39433e7a895a53449ab616f Mon Sep 17 00:00:00 2001 From: Casper van Elteren Date: Thu, 3 Dec 2026 11:15:02 +0100 Subject: [PATCH 1/2] Fix alpha handling for positive and negative bar containers --- ultraplot/axes/plot.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/ultraplot/axes/plot.py b/ultraplot/axes/plot.py index fc17d3321..0ab90d25b 100644 --- a/ultraplot/axes/plot.py +++ b/ultraplot/axes/plot.py @@ -6236,8 +6236,10 @@ def _apply_bar( self._inbounds_xylim(extents, x, y, orientation=orientation) if alphas[i] is not None: - for child in obj.get_children(): - child.set_alpha(alphas[i]) + # With negpos, each sign has its own BarContainer. + for container in obj if negpos else (obj,): + for child in container.get_children(): + child.set_alpha(alphas[i]) objs.append((*eb, obj) if eb else obj) self._update_guide(objs, **guide_kw) From 573d5b373e2b98bb6afdfb3c3bea41b270fa5cd7 Mon Sep 17 00:00:00 2001 From: Casper van Elteren Date: Thu, 3 Dec 2026 11:15:18 +0100 Subject: [PATCH 2/2] Add regression tests for negpos bar alpha in both orientations --- ultraplot/tests/test_1dplots.py | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/ultraplot/tests/test_1dplots.py b/ultraplot/tests/test_1dplots.py index 4108d1e09..978bb8268 100644 --- a/ultraplot/tests/test_1dplots.py +++ b/ultraplot/tests/test_1dplots.py @@ -1012,6 +1012,37 @@ def test_bar_alpha(rng): uplt.close(fig) +@pytest.mark.parametrize("method", ["bar", "barh"]) +@pytest.mark.parametrize("alpha", [0, 0.5, 1]) +def test_bar_negpos_alpha(method, alpha): + """Apply scalar alpha to both sign containers (issue #865).""" + fig, ax = uplt.subplots() + bars = getattr(ax, method)([1, 2, 3], [1, -1, 2], negpos=True, alpha=alpha) + assert len(bars) == 2 + for container in bars: + assert len(container.patches) == 3 + assert all(patch.get_alpha() == alpha for patch in container.patches) + fig.canvas.draw() + uplt.close(fig) + + +@pytest.mark.parametrize("method", ["bar", "barh"]) +def test_bar_negpos_column_alpha(method): + """Apply each column's alpha to both of its sign containers.""" + fig, ax = uplt.subplots() + values = np.array([[1, -2], [-1, 3], [2, -1]]) + alphas = [0.2, 0.8] + bars = getattr(ax, method)([1, 2, 3], values, negpos=True, alpha=alphas) + assert len(bars) == len(alphas) + for group, alpha in zip(bars, alphas): + assert len(group) == 2 + for container in group: + assert len(container.patches) == 3 + assert all(patch.get_alpha() == alpha for patch in container.patches) + fig.canvas.draw() + uplt.close(fig) + + @pytest.mark.mpl_image_compare def test_lollipop_graph(rng): """