Skip to content

Commit ce6ac2d

Browse files
authored
Merge pull request matplotlib#18288 from jklymak/fix-check-title-off-page
FIX: check if axes is off page before repositioning title
2 parents 13e3573 + a9ac3d9 commit ce6ac2d

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

lib/matplotlib/axes/_base.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2630,7 +2630,6 @@ def _update_title_position(self, renderer):
26302630
Update the title position based on the bounding box enclosing
26312631
all the ticklabels and x-axis spine and xlabel...
26322632
"""
2633-
26342633
if self._autotitlepos is not None and not self._autotitlepos:
26352634
_log.debug('title position was updated manually, not adjusting')
26362635
return
@@ -2653,7 +2652,7 @@ def _update_title_position(self, renderer):
26532652
else:
26542653
ax.apply_aspect()
26552654
axs = axs + [ax]
2656-
top = 0
2655+
top = -np.Inf
26572656
for ax in axs:
26582657
if (ax.xaxis.get_ticks_position() in ['top', 'unknown']
26592658
or ax.xaxis.get_label_position() == 'top'):
@@ -2662,6 +2661,11 @@ def _update_title_position(self, renderer):
26622661
bb = ax.get_window_extent(renderer)
26632662
if bb is not None:
26642663
top = max(top, bb.ymax)
2664+
if top < 0:
2665+
# the top of axes is not even on the figure, so don't try and
2666+
# automatically place it.
2667+
_log.debug('top of axes not in the figure, so title not moved')
2668+
return
26652669
if title.get_window_extent(renderer).ymin < top:
26662670
_, y = self.transAxes.inverted().transform((0, top))
26672671
title.set_position((x, y))

lib/matplotlib/tests/test_axes.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5544,6 +5544,19 @@ def test_title_xticks_top_both():
55445544
assert ax.title.get_position()[1] > 1.04
55455545

55465546

5547+
def test_title_no_move_off_page():
5548+
# If an axes is off the figure (ie. if it is cropped during a save)
5549+
# make sure that the automatic title repositioning does not get done.
5550+
mpl.rcParams['axes.titley'] = None
5551+
fig = plt.figure()
5552+
ax = fig.add_axes([0.1, -0.5, 0.8, 0.2])
5553+
ax.tick_params(axis="x",
5554+
bottom=True, top=True, labelbottom=True, labeltop=True)
5555+
tt = ax.set_title('Boo')
5556+
fig.canvas.draw()
5557+
assert tt.get_position()[1] == 1.0
5558+
5559+
55475560
def test_offset_label_color():
55485561
# Tests issue 6440
55495562
fig = plt.figure()

0 commit comments

Comments
 (0)