Skip to content

Commit ea157d7

Browse files
authored
Merge pull request matplotlib#31885 from gaoflow/fix-31881-hidden-offset-title-nan
Skip hidden y-axis offset text when positioning titles (fix matplotlib#31881)
2 parents d421cec + 80b56f9 commit ea157d7

2 files changed

Lines changed: 22 additions & 1 deletion

File tree

lib/matplotlib/axes/_base.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3261,7 +3261,12 @@ def _update_title_position(self, renderer):
32613261
if title.get_text():
32623262
for ax in axs:
32633263
ax.yaxis.get_tightbbox(renderer) # update offsetText
3264-
if ax.yaxis.offsetText.get_text():
3264+
# A hidden offset text (e.g. on the shared y axis of an
3265+
# inner subplot) is not drawn, so it must not move the
3266+
# title: its tight bbox is non-finite and would otherwise
3267+
# push the title to infinity.
3268+
if (ax.yaxis.offsetText.get_visible()
3269+
and ax.yaxis.offsetText.get_text()):
32653270
bb = ax.yaxis.offsetText.get_tightbbox(renderer)
32663271
if bb.intersection(title.get_tightbbox(renderer), bb):
32673272
top = bb.ymax

lib/matplotlib/tests/test_axes.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8009,6 +8009,22 @@ def test_title_above_offset(left, center):
80098009
assert ycenter == yleft
80108010

80118011

8012+
def test_title_above_hidden_offset():
8013+
# On an inner subplot with a shared y axis the offset text is hidden, but
8014+
# it still carries text. It must not be considered when positioning the
8015+
# title: its tight bbox is non-finite and used to push the title (and the
8016+
# subplot position computed by tight_layout) to NaN/inf. See #31881.
8017+
mpl.rcParams['axes.titley'] = None
8018+
fig, axs = plt.subplots(1, 2, sharey=True, tight_layout=True)
8019+
for i, ax in enumerate(axs):
8020+
ax.set_title(f'Subplot {i}')
8021+
ax.plot(range(10), [1e53] * 10)
8022+
fig.draw_without_rendering() # used to raise ValueError (NaN -> int)
8023+
for ax in axs:
8024+
assert np.isfinite(ax.title.get_window_extent().ymin)
8025+
assert np.all(np.isfinite(ax.get_position().bounds))
8026+
8027+
80128028
def test_title_no_move_off_page():
80138029
# If an Axes is off the figure (ie. if it is cropped during a save)
80148030
# make sure that the automatic title repositioning does not get done.

0 commit comments

Comments
 (0)