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
Expand file treeCollapse file tree
Open diff view settings
Collapse file
Copy file name to clipboardExpand all lines: lib/matplotlib/axes/_base.py +6-1Lines changed: 6 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -3261,7 +3261,12 @@ def _update_title_position(self, renderer): |
3261 | 3261 | if title.get_text(): |
3262 | 3262 | for ax in axs: |
3263 | 3263 | 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()): |
3265 | 3270 | bb = ax.yaxis.offsetText.get_tightbbox(renderer) |
3266 | 3271 | if bb.intersection(title.get_tightbbox(renderer), bb): |
3267 | 3272 | top = bb.ymax |
|
Collapse file
Copy file name to clipboardExpand all lines: lib/matplotlib/tests/test_axes.py +16Lines changed: 16 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -8009,6 +8009,22 @@ def test_title_above_offset(left, center): |
8009 | 8009 | assert ycenter == yleft |
8010 | 8010 |
|
8011 | 8011 |
|
| 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 | + |
8012 | 8028 | def test_title_no_move_off_page(): |
8013 | 8029 | # If an Axes is off the figure (ie. if it is cropped during a save) |
8014 | 8030 | # make sure that the automatic title repositioning does not get done. |
|