From 62a839b389b43f89c36566335d3a49f67dfd9d42 Mon Sep 17 00:00:00 2001 From: Antony Lee Date: Mon, 25 Mar 2019 10:27:43 +0100 Subject: [PATCH] Backport PR #13741: FIX: make title move above ticklabels --- lib/matplotlib/axes/_base.py | 5 +++-- lib/matplotlib/tests/test_axes.py | 10 ++++++++++ 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/lib/matplotlib/axes/_base.py b/lib/matplotlib/axes/_base.py index 26d8df32e396..b0e5ed36989e 100644 --- a/lib/matplotlib/axes/_base.py +++ b/lib/matplotlib/axes/_base.py @@ -2536,8 +2536,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 34d9cc66e709..12aef65beeaf 100644 --- a/lib/matplotlib/tests/test_axes.py +++ b/lib/matplotlib/tests/test_axes.py @@ -5724,6 +5724,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()