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

Skip to content

Faster title alignment #28300

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 18 additions & 11 deletions lib/matplotlib/axes/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -2985,6 +2985,10 @@ def _update_title_position(self, renderer):

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

if not any(title.get_text() for title in titles):
# If the titles are all empty, there is no need to update their positions.
return

# Need to check all our twins too, aligned axes, and all the children
# as well.
axs = set()
Expand All @@ -2996,21 +3000,24 @@ def _update_title_position(self, renderer):
locator = ax.get_axes_locator()
ax.apply_aspect(locator(self, renderer) if locator else None)

top = -np.inf
for ax in axs:
bb = None
xticklabel_top = any(tick.label2.get_visible() for tick in
[ax.xaxis.majorTicks[0], ax.xaxis.minorTicks[0]])
Comment on lines +3006 to +3007
Copy link
Member Author

@rcomer rcomer May 27, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Directly check the visibility of the tick label as I think that is the most relevant part from xaxis.get_ticks_position() and will prevent the unnecessary calls to xaxis.get_tightbbox mentioned at #26150 (comment). Only checking the first tick is consistent with get_ticks_position.

There is a question in my mind about what happens if the ticks are pointing outward but unlabelled, but I think that could already be an issue with the existing approach as you could have "default" position and outward ticks. Edit: the ticks themselves aren't included in get_tightbbox so actually this makes no difference.

if (xticklabel_top or ax.xaxis.get_label_position() == 'top'):
bb = ax.xaxis.get_tightbbox(renderer)
if bb is None:
# Extent of the outline for colorbars, of the axes otherwise.
bb = ax.spines.get("outline", ax).get_window_extent()
top = max(top, bb.ymax)

for title in titles:
x, _ = title.get_position()
# need to start again in case of window resizing
title.set_position((x, 1.0))
top = -np.inf
for ax in axs:
bb = None
if (ax.xaxis.get_ticks_position() in ['top', 'unknown']
or ax.xaxis.get_label_position() == 'top'):
bb = ax.xaxis.get_tightbbox(renderer)
if bb is None:
# Extent of the outline for colorbars, of the axes otherwise.
bb = ax.spines.get("outline", ax).get_window_extent()
top = max(top, bb.ymax)
if title.get_text():
if title.get_text():
for ax in axs:
ax.yaxis.get_tightbbox(renderer) # update offsetText
if ax.yaxis.offsetText.get_text():
bb = ax.yaxis.offsetText.get_tightbbox(renderer)
Expand Down
12 changes: 12 additions & 0 deletions lib/matplotlib/tests/test_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -7118,6 +7118,18 @@ def test_title_no_move_off_page():
assert tt.get_position()[1] == 1.0


def test_title_inset_ax():
# Title should be above any child axes
mpl.rcParams['axes.titley'] = None
fig, ax = plt.subplots()
ax.set_title('Title')
fig.draw_without_rendering()
assert ax.title.get_position()[1] == 1
ax.inset_axes([0, 1, 1, 0.1])
fig.draw_without_rendering()
assert ax.title.get_position()[1] == 1.1


def test_offset_label_color():
# Tests issue 6440
fig, ax = plt.subplots()
Expand Down
Loading