GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Skip hidden offset text when positioning Axes titles · matplotlib/matplotlib@80b56f9

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • lib/matplotlib
    • axes
      • _base.py
    • tests
      • test_axes.py
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎lib/matplotlib/axes/_base.py‎

Copy file name to clipboardExpand all lines: lib/matplotlib/axes/_base.py
+6-1Lines 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
Collapse file

‎lib/matplotlib/tests/test_axes.py‎

Copy file name to clipboardExpand all lines: lib/matplotlib/tests/test_axes.py
+16Lines 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 (0)
You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.