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

Skip to content

Commit 8abe308

Browse files
committed
Faster title alignment
1 parent 2834da1 commit 8abe308

File tree

1 file changed

+18
-11
lines changed

1 file changed

+18
-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)

0 commit comments

Comments
 (0)