8000 Merge pull request #18297 from meeseeksmachine/auto-backport-of-pr-18… · matplotlib/matplotlib@355fac9 · GitHub
[go: up one dir, main page]

Skip to content

Commit 355fac9

Browse files
authored
Merge pull request #18297 from meeseeksmachine/auto-backport-of-pr-18288-on-v3.3.x
Backport PR #18288 on branch v3.3.x (FIX: check if axes is off page before repositioning title)
2 parents fe6a433 + 6a2fcd1 commit 355fac9

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
@@ -2614,7 +2614,6 @@ def _update_title_position(self, renderer):
26142614
Update the title position based on the bounding box enclosing
26152615
all the ticklabels and x-axis spine and xlabel...
26162616
"""
2617-
26182617
if self._autotitlepos is not None and not self._autotitlepos:
26192618
_log.debug('title position was updated manually, not adjusting')
26202619
return
@@ -2637,7 +2636,7 @@ def _update_title_position(self, renderer):
26372636
else:
26382637
ax.apply_aspect()
26392638
axs = axs + [ax]
2640-
top = 0
2639+
top = -np.Inf
26412640
for ax in axs:
26422641
if (ax.xaxis.get_ticks_position() in ['top', 'unknown']
26432642
or ax.xaxis.get_label_position() == 'top'):
@@ -2646,6 +2645,11 @@ def _update_title_position(self, renderer):
26462645
bb = ax.get_window_extent(renderer)
26472646
if bb is not None:
26482647
top = max(top, bb.ymax)
2648+
if top < 0:
2649+
# the top of axes is not even on the figure, so don't try and
2650+
# automatically place it.
2651+
_log.debug('top of axes not in the figure, so title not moved')
2652+
return
26492653
if title.get_window_extent(renderer).ymin < top:
26502654
_, y = self.transAxes.inverted().transform((0, top))
26512655
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
@@ -5430,6 +5430,19 @@ def test_title_xticks_top_both():
54305430
assert ax.title.get_position()[1] > 1.04
54315431

54325432

5433+
def test_title_no_move_off_page():
5434+
# If an axes is off the figure (ie. if it is cropped during a save)
5435+
# make sure that the automatic title repositioning does not get done.
5436+
mpl.rcParams['axes.titley'] = None
5437+
fig = plt.figure()
5438+
ax = fig.add_axes([0.1, -0.5, 0.8, 0.2])
5439+
ax.tick_params(axis="x",
5440+
bottom=True, top=True, labelbottom=True, labeltop=True)
5441+
tt = ax.set_title('Boo')
5442+
fig.canvas.draw()
5443+
assert tt.get_position()[1] == 1.0
5444+
5445+
54335446
def test_offset_label_color():
54345447
# Tests issue 6440
54355448
fig = plt.figure()

0 commit comments

Comments
 (0)
0