diff --git a/lib/matplotlib/axes/_base.py b/lib/matplotlib/axes/_base.py index 95d534bf1507..a61be157e012 100644 --- a/lib/matplotlib/axes/_base.py +++ b/lib/matplotlib/axes/_base.py @@ -2535,8 +2535,9 @@ def _update_title_position(self, renderer): top = 0 for ax in axs: try: - if (ax.xaxis.get_label_position() == 'top' - or ax.xaxis.get_ticks_position() == 'top'): + choices = ['top', 'unknown'] + if (ax.xaxis.get_label_position() == 'top' or + ax.xaxis.get_ticks_position() in choices): bb = ax.xaxis.get_tightbbox(renderer) else: bb = ax.get_window_extent(renderer) diff --git a/lib/matplotlib/tests/test_axes.py b/lib/matplotlib/tests/test_axes.py index cf29daba9ae3..741a9c2b574a 100644 --- a/lib/matplotlib/tests/test_axes.py +++ b/lib/matplotlib/tests/test_axes.py @@ -5723,6 +5723,16 @@ def test_title_xticks_top(): assert ax.title.get_position()[1] > 1.04 +def test_title_xticks_top_both(): + # Test that title moves if xticks on top of axes. + fig, ax = plt.subplots() + ax.tick_params(axis="x", bottom=True, top=True, + labelbottom=True, labeltop=True) + ax.set_title('xlabel top') + fig.canvas.draw() + assert ax.title.get_position()[1] > 1.04 + + def test_offset_label_color(): # Tests issue 6440 fig = plt.figure()