From f0e7c70a8bafff2ac8024c7e2c79f5c7be5cad1f Mon Sep 17 00:00:00 2001 From: Jody Klymak Date: Tue, 18 Aug 2020 09:43:36 -0700 Subject: [PATCH 1/3] FIX: check if axes is off page before repositioning title --- lib/matplotlib/axes/_base.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/lib/matplotlib/axes/_base.py b/lib/matplotlib/axes/_base.py index 045e68925766..ca00f96a0e1c 100644 --- a/lib/matplotlib/axes/_base.py +++ b/lib/matplotlib/axes/_base.py @@ -2632,7 +2632,6 @@ def _update_title_position(self, renderer): Update the title position based on the bounding box enclosing all the ticklabels and x-axis spine and xlabel... """ - if self._autotitlepos is not None and not self._autotitlepos: _log.debug('title position was updated manually, not adjusting') return @@ -2655,7 +2654,7 @@ def _update_title_position(self, renderer): else: ax.apply_aspect() axs = axs + [ax] - top = 0 + top = -np.Inf for ax in axs: if (ax.xaxis.get_ticks_position() in ['top', 'unknown'] or ax.xaxis.get_label_position() == 'top'): @@ -2664,6 +2663,11 @@ def _update_title_position(self, renderer): bb = ax.get_window_extent(renderer) if bb is not None: top = max(top, bb.ymax) + if top < 0 or top > 1: + # the top of axes is not even on the figure, so don't try and + # automatically place it. + _log.debug('top of axes not in the figure, so title not moved') + return if title.get_window_extent(renderer).ymin < top: _, y = self.transAxes.inverted().transform((0, top)) title.set_position((x, y)) From de2336bb01e2e8faf2d167348051f9600528c1b5 Mon Sep 17 00:00:00 2001 From: Jody Klymak Date: Tue, 18 Aug 2020 10:08:25 -0700 Subject: [PATCH 2/3] TST: Add a test for off page --- lib/matplotlib/axes/_base.py | 2 +- lib/matplotlib/tests/test_axes.py | 13 +++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/lib/matplotlib/axes/_base.py b/lib/matplotlib/axes/_base.py index ca00f96a0e1c..3c383ef71970 100644 --- a/lib/matplotlib/axes/_base.py +++ b/lib/matplotlib/axes/_base.py @@ -2663,7 +2663,7 @@ def _update_title_position(self, renderer): bb = ax.get_window_extent(renderer) if bb is not None: top = max(top, bb.ymax) - if top < 0 or top > 1: + if top < 0: # the top of axes is not even on the figure, so don't try and # automatically place it. _log.debug('top of axes not in the figure, so title not moved') diff --git a/lib/matplotlib/tests/test_axes.py b/lib/matplotlib/tests/test_axes.py index 84b13405af33..664a478bfe63 100644 --- a/lib/matplotlib/tests/test_axes.py +++ b/lib/matplotlib/tests/test_axes.py @@ -5533,6 +5533,19 @@ def test_title_xticks_top_both(): assert ax.title.get_position()[1] > 1.04 +def test_title_no_move_off_page(): + # If an axes is off the figure (ie. if it is cropped during a save) + # make sure that the automatic title repositioning does not get done. + mpl.rcParams['axes.titley'] = None + fig = plt.figure() + ax = fig.add_axes([0.1, -0.5, 0.8, 0.2]) + ax.tick_params(axis="x", + bottom=True, top=True, labelbottom=True, labeltop=True) + tt = ax.set_title('Boo') + fig.canvas.draw() + assert tt.get_position()[1] == 1.0 + + def test_offset_label_color(): # Tests issue 6440 fig = plt.figure() From a9ac3d9a902ba72f828654b6960047fb82e07fe6 Mon Sep 17 00:00:00 2001 From: Jody Klymak Date: Tue, 18 Aug 2020 10:09:16 -0700 Subject: [PATCH 3/3] FLK8: fix flae8 complaint: unrelated to this PR --- lib/matplotlib/tests/test_pickle.py | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/matplotlib/tests/test_pickle.py b/lib/matplotlib/tests/test_pickle.py index 4ab73e402076..c302bbbab98a 100644 --- a/lib/matplotlib/tests/test_pickle.py +++ b/lib/matplotlib/tests/test_pickle.py @@ -1,6 +1,5 @@ from io import BytesIO import pickle -import platform import numpy as np import pytest