Skip to content

Commit e7b350a

Browse files
committed
Faster title alignment
1 parent 00c82ce commit e7b350a

File tree

1 file changed

+20
-14
lines changed

1 file changed

+20
-14
lines changed

lib/matplotlib/axes/_base.py

Lines changed: 20 additions & 14 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,24 +3000,26 @@ 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+
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+
if 'outline' in ax.spines:
3011+
# Special case for colorbars:
3012+
bb = ax.spines['outline'].get_window_extent()
3013+
else:
3014+
bb = ax.get_window_extent(renderer)
3015+
top = max(top, bb.ymax)
3016+
29993017
for title in titles:
30003018
x, _ = title.get_position()
30013019
# need to start again in case of window resizing
30023020
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-
if 'outline' in ax.spines:
3011-
# Special case for colorbars:
3012-
bb = ax.spines['outline'].get_window_extent()
3013-
else:
3014-
bb = ax.get_window_extent(renderer)
3015-
top = max(top, bb.ymax)
3016-
if title.get_text():
3021+
if title.get_text():
3022+
for ax in axs:
30173023
ax.yaxis.get_tightbbox(renderer) # update offsetText
30183024
if ax.yaxis.offsetText.get_text():
30193025
bb = ax.yaxis.offsetText.get_tightbbox(renderer)

0 commit comments

Comments
 (0)