From 786812e27cf88ffa6f87f7efd2f009838bf25ff5 Mon Sep 17 00:00:00 2001 From: Kyle Sunden Date: Mon, 17 Apr 2023 00:48:36 -0500 Subject: [PATCH] Backport PR #25681: BUG: Return null Bbox when there is no intersection for bar_label center. --- lib/matplotlib/axes/_axes.py | 2 +- lib/matplotlib/tests/test_axes.py | 13 +++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) 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]