diff --git a/lib/matplotlib/axes/_base.py b/lib/matplotlib/axes/_base.py index 2f0f8865280f..f383aba762fc 100644 --- a/lib/matplotlib/axes/_base.py +++ b/lib/matplotlib/axes/_base.py @@ -2981,13 +2981,13 @@ def _update_title_position(self, renderer): axs = axs + [ax] top = -np.Inf for ax in axs: + bb = None if (ax.xaxis.get_ticks_position() in ['top', 'unknown'] or ax.xaxis.get_label_position() == 'top'): bb = ax.xaxis.get_tightbbox(renderer) - else: + if bb is None: bb = ax.get_window_extent(renderer) - if bb is not None: - top = max(top, bb.ymax) + top = max(top, bb.ymax) if top < 0: # the top of Axes is not even on the figure, so don't try and # automatically place it. diff --git a/lib/matplotlib/tests/test_axes.py b/lib/matplotlib/tests/test_axes.py index 1b349b2a9c31..151377b26728 100644 --- a/lib/matplotlib/tests/test_axes.py +++ b/lib/matplotlib/tests/test_axes.py @@ -5627,6 +5627,17 @@ def test_title_location_roundtrip(): ax.set_title('fail', loc='foo') +@pytest.mark.parametrize('sharex', [True, False]) +def test_title_location_shared(sharex): + fig, axs = plt.subplots(2, 1, sharex=sharex) + axs[0].set_title('A', pad=-40) + axs[1].set_title('B', pad=-40) + fig.draw_without_rendering() + x, y1 = axs[0].title.get_position() + x, y2 = axs[1].title.get_position() + assert y1 == y2 == 1.0 + + @image_comparison(["loglog.png"], remove_text=True, tol=0.02) def test_loglog(): fig, ax = plt.subplots()