From 859993bfcf03e8f3fd2ef5151ff589354e31c7c5 Mon Sep 17 00:00:00 2001 From: Stefanie Molin <24376333+stefmolin@users.noreply.github.com> Date: Thu, 13 Apr 2023 20:32:30 -0400 Subject: [PATCH 1/3] BUG: Return null Bbox when there is no intersection for bar_label center. --- lib/matplotlib/axes/_axes.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/matplotlib/axes/_axes.py b/lib/matplotlib/axes/_axes.py index e712e1302544..cd03957dbbfa 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": From 17f0ef3a19b4cd5bb7f702af0b9056a2ba089a37 Mon Sep 17 00:00:00 2001 From: Stefanie Molin <24376333+stefmolin@users.noreply.github.com> Date: Thu, 13 Apr 2023 20:59:17 -0400 Subject: [PATCH 2/3] TST: Add test case for bug fix. --- lib/matplotlib/tests/test_axes.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/lib/matplotlib/tests/test_axes.py b/lib/matplotlib/tests/test_axes.py index 3c138f46e010..35efca4b69f7 100644 --- a/lib/matplotlib/tests/test_axes.py +++ b/lib/matplotlib/tests/test_axes.py @@ -8040,6 +8040,19 @@ def test_centered_bar_label_nonlinear(): ax.set_axis_off() +def test_centered_bar_label_label_beyond_limits(tmp_path): + _, 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) + + plt.savefig(tmp_path / 'test') + + def test_bar_label_location_errorbars(): ax = plt.gca() xs, heights = [1, 2], [3, -4] From 7037e858c781a810b003d37fbf20a42a8c14ccea Mon Sep 17 00:00:00 2001 From: Stefanie Molin <24376333+stefmolin@users.noreply.github.com> Date: Sat, 15 Apr 2023 14:44:21 -0400 Subject: [PATCH 3/3] TST: Switch to fig.draw_without_rendering() in test. --- lib/matplotlib/tests/test_axes.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/matplotlib/tests/test_axes.py b/lib/matplotlib/tests/test_axes.py index 35efca4b69f7..863e3e2f86a6 100644 --- a/lib/matplotlib/tests/test_axes.py +++ b/lib/matplotlib/tests/test_axes.py @@ -8040,8 +8040,8 @@ def test_centered_bar_label_nonlinear(): ax.set_axis_off() -def test_centered_bar_label_label_beyond_limits(tmp_path): - _, ax = plt.subplots() +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]): @@ -8050,7 +8050,7 @@ def test_centered_bar_label_label_beyond_limits(tmp_path): last += value ax.set_xlim(None, 20) - plt.savefig(tmp_path / 'test') + fig.draw_without_rendering() def test_bar_label_location_errorbars():