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

Skip to content

Commit 9a9fce5

Browse files
authored
Merge pull request matplotlib#28300 from rcomer/empty-titles
Faster title alignment
2 parents 2834da1 + 1dfc773 commit 9a9fce5

File tree

2 files changed

+30
-11
lines changed

2 files changed

+30
-11
lines changed

lib/matplotlib/axes/_base.py

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2985,6 +2985,10 @@ def _update_title_position(self, renderer):
29852985

29862986
titles = (self.title, self._left_title, self._right_title)
29872987

2988+
if not any(title.get_text() for title in titles):
2989+
# If the titles are all empty, there is no need to update their positions.
2990+
return
2991+
29882992
# Need to check all our twins too, aligned axes, and all the children
29892993
# as well.
29902994
axs = set()
@@ -2996,21 +3000,24 @@ def _update_title_position(self, renderer):
29963000
locator = ax.get_axes_locator()
29973001
ax.apply_aspect(locator(self, renderer) if locator else None)
29983002

3003+
top = -np.inf
3004+
for ax in axs:
3005+
bb = None
3006+
xticklabel_top = any(tick.label2.get_visible() for tick in
3007+
[ax.xaxis.majorTicks[0], ax.xaxis.minorTicks[0]])
3008+
if (xticklabel_top or ax.xaxis.get_label_position() == 'top'):
3009+
bb = ax.xaxis.get_tightbbox(renderer)
3010+
if bb is None:
3011+
# Extent of the outline for colorbars, of the axes otherwise.
3012+
bb = ax.spines.get("outline", ax).get_window_extent()
3013+
top = max(top, bb.ymax)
3014+
29993015
for title in titles:
30003016
x, _ = title.get_position()
30013017
# need to start again in case of window resizing
30023018
title.set_position((x, 1.0))
3003-
top = -np.inf
3004-
for ax in axs:
3005-
bb = None
3006-
if (ax.xaxis.get_ticks_position() in ['top', 'unknown']
3007-
or ax.xaxis.get_label_position() == 'top'):
3008-
bb = ax.xaxis.get_tightbbox(renderer)
3009-
if bb is None:
3010-
# Extent of the outline for colorbars, of the axes otherwise.
3011-
bb = ax.spines.get("outline", ax).get_window_extent()
3012-
top = max(top, bb.ymax)
3013-
if title.get_text():
3019+
if title.get_text():
3020+
for ax in axs:
30143021
ax.yaxis.get_tightbbox(renderer) # update offsetText
30153022
if ax.yaxis.offsetText.get_text():
30163023
bb = ax.yaxis.offsetText.get_tightbbox(renderer)

lib/matplotlib/tests/test_axes.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7118,6 +7118,18 @@ def test_title_no_move_off_page():
71187118
assert tt.get_position()[1] == 1.0
71197119

71207120

7121+
def test_title_inset_ax():
7122+
# Title should be above any child axes
7123+
mpl.rcParams['axes.titley'] = None
7124+
fig, ax = plt.subplots()
7125+
ax.set_title('Title')
7126+
fig.draw_without_rendering()
7127+
assert ax.title.get_position()[1] == 1
7128+
ax.inset_axes([0, 1, 1, 0.1])
7129+
fig.draw_without_rendering()
7130+
assert ax.title.get_position()[1] == 1.1
7131+
7132+
71217133
def test_offset_label_color():
71227134
# Tests issue 6440
71237135
fig, ax = plt.subplots()

0 commit comments

Comments
 (0)