diff --git a/lib/matplotlib/axes/_axes.py b/lib/matplotlib/axes/_axes.py index f17efbbd77cc..54147d0449f3 100644 --- a/lib/matplotlib/axes/_axes.py +++ b/lib/matplotlib/axes/_axes.py @@ -2769,7 +2769,7 @@ def sign(x): lambda r, b=bar: mtransforms.Bbox.intersection( b.get_window_extent(r), b.get_clip_box() - ) + ) or mtransforms.Bbox.null() ) else: # edge if orientation == "vertical": diff --git a/lib/matplotlib/tests/test_axes.py b/lib/matplotlib/tests/test_axes.py index 004f6320de1f..33ab0ea9b321 100644 --- a/lib/matplotlib/tests/test_axes.py +++ b/lib/matplotlib/tests/test_axes.py @@ -8008,6 +8008,19 @@ def test_centered_bar_label_nonlinear(): ax.set_axis_off() +def test_centered_bar_label_label_beyond_limits(): + fig, ax = plt.subplots() + + last = 0 + for label, value in zip(['a', 'b', 'c'], [10, 20, 50]): + bar_container = ax.barh('col', value, label=label, left=last) + ax.bar_label(bar_container, label_type='center') + last += value + ax.set_xlim(None, 20) + + fig.draw_without_rendering() + + def test_bar_label_location_errorbars(): ax = plt.gca() xs, heights = [1, 2], [3, -4]