diff --git a/lib/matplotlib/axis.py b/lib/matplotlib/axis.py index d80e7b4dafb9..ba17c634ac5c 100644 --- a/lib/matplotlib/axis.py +++ b/lib/matplotlib/axis.py @@ -1333,9 +1333,11 @@ def _update_ticks(self): def _get_ticklabel_bboxes(self, ticks, renderer): """Return lists of bboxes for ticks' label1's and label2's.""" return ([tick.label1.get_window_extent(renderer) - for tick in ticks if tick.label1.get_visible()], + for tick in ticks + if tick.label1.get_visible() and tick.label1.get_in_layout()], [tick.label2.get_window_extent(renderer) - for tick in ticks if tick.label2.get_visible()]) + for tick in ticks + if tick.label2.get_visible() and tick.label2.get_in_layout()]) def get_tightbbox(self, renderer=None, *, for_layout_only=False): """ diff --git a/lib/matplotlib/tests/test_axis.py b/lib/matplotlib/tests/test_axis.py index 97884a33208f..67d9ed5bde62 100644 --- a/lib/matplotlib/tests/test_axis.py +++ b/lib/matplotlib/tests/test_axis.py @@ -2,6 +2,7 @@ import matplotlib.pyplot as plt from matplotlib.axis import XTick +from matplotlib.testing.decorators import check_figures_equal def test_tick_labelcolor_array(): @@ -31,6 +32,21 @@ def test_axis_not_in_layout(): assert ax1_right.get_position().bounds == ax2_right.get_position().bounds +@check_figures_equal() +def test_tick_not_in_layout(fig_test, fig_ref): + # Check that the "very long" ticklabel is ignored from layouting after + # set_in_layout(False); i.e. the layout is as if the ticklabel was empty. + # Ticklabels are set to white so that the actual string doesn't matter. + fig_test.set_layout_engine("constrained") + ax = fig_test.add_subplot(xticks=[0, 1], xticklabels=["short", "very long"]) + ax.tick_params(labelcolor="w") + fig_test.draw_without_rendering() # Ensure ticks are correct. + ax.xaxis.majorTicks[-1].label1.set_in_layout(False) + fig_ref.set_layout_engine("constrained") + ax = fig_ref.add_subplot(xticks=[0, 1], xticklabels=["short", ""]) + ax.tick_params(labelcolor="w") + + def test_translate_tick_params_reverse(): fig, ax = plt.subplots() kw = {'label1On': 'a', 'label2On': 'b', 'tick1On': 'c', 'tick2On': 'd'}