Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 862b4a1

Browse files
committed
FIX: Autoposition title when yaxis has offset
Move the title above the offset text if present.
1 parent ac2a145 commit 862b4a1

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed

lib/matplotlib/axes/_base.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2990,6 +2990,10 @@ def _update_title_position(self, renderer):
29902990
if bb is None:
29912991
bb = ax.get_window_extent(renderer)
29922992
top = max(top, bb.ymax)
2993+
# take yaxis offset text into account
2994+
bb = ax.yaxis.get_tightbbox(renderer)
2995+
if ax.yaxis.offsetText.get_text() and bb.ymax > top:
2996+
top = bb.ymax
29932997
if top < 0:
29942998
# the top of Axes is not even on the figure, so don't try and
29952999
# automatically place it.

lib/matplotlib/tests/test_axes.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6121,6 +6121,16 @@ def test_title_xticks_top_both():
61216121
assert ax.title.get_position()[1] > 1.04
61226122

61236123

6124+
def test_title_above_offset():
6125+
# Test that title moves if yaxis has offset text.
6126+
mpl.rcParams['axes.titley'] = None
6127+
fig, ax = plt.subplots()
6128+
ax.set_ylim(1e11)
6129+
ax.set_title('Title')
6130+
fig.draw_without_rendering()
6131+
assert ax.title.get_position()[1] > 1.0
6132+
6133+
61246134
def test_title_no_move_off_page():
61256135
# If an axes is off the figure (ie. if it is cropped during a save)
61266136
# make sure that the automatic title repositioning does not get done.

0 commit comments

Comments
 (0)