Skip to content

Commit 80b56f9

Browse files
committed
Skip hidden offset text when positioning Axes titles
The y-axis offset text of an inner subplot that shares its y axis is hidden but still carries text. _update_title_position only checked get_text(), so since matplotlib#31285 (which makes a hidden text report a null, non-finite tight bbox) the offset text pushed the title -- and the subplot position computed by tight_layout -- to inf/NaN. Drawing then raised "cannot convert float NaN to integer". Only consider the offset text for title placement when it is visible.
1 parent d421cec commit 80b56f9

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)