Skip to content

Commit 44705a8

Browse files
author
Your Name
committed
BUG: Fix label positioning in stacked bar plots with zero-height bars (GH#59429)
1 parent e49ab80 commit 44705a8

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

pandas/plotting/_matplotlib/core.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1960,7 +1960,7 @@ def _make_plot(self, fig: Figure) -> None:
19601960
)
19611961
ax.set_title(label)
19621962
elif self.stacked:
1963-
mask = y > 0
1963+
mask = y >= 0
19641964
start = np.where(mask, pos_prior, neg_prior) + self._start_base
19651965
w = self.bar_width / 2
19661966
rect = self._plot(

pandas/tests/plotting/frame/test_frame.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -774,6 +774,15 @@ def test_bar_nan_stacked(self):
774774
expected = [0.0, 0.0, 0.0, 10.0, 0.0, 20.0, 15.0, 10.0, 40.0]
775775
assert result == expected
776776

777+
def test_bar_stacked_label_position_with_zero_height(self):
778+
df = pd.DataFrame({"A": [3, 0, 1], "B": [0, 2, 4], "C": [5, 0, 2]})
779+
ax = df.plot.bar(stacked=True)
780+
ax.bar_label(ax.containers[-1])
781+
expected = [8., 2., 7.]
782+
result = [text.xy[1] for text in ax.texts]
783+
tm.assert_almost_equal(result, expected)
784+
plt.close("all")
785+
777786
@pytest.mark.parametrize("idx", [Index, pd.CategoricalIndex])
778787
def test_bar_categorical(self, idx):
779788
# GH 13019

0 commit comments

Comments
 (0)