Skip to content
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
12 changes: 10 additions & 2 deletions ultraplot/axes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -2927,10 +2927,18 @@ def _update_title_position(self, renderer):
if base_x >= ax1 + abc_title_sep:
max_width = base_x - (ax1 + abc_title_sep)
elif ha == "center":
# Keep the requested font size for centered titles and
# resolve collisions by shifting the title away from the
# abc label, matching the overflow-tolerant behavior of
# left/right titles in practice.
if base_x >= ax1 + abc_title_sep:
max_width = 2 * (base_x - (ax1 + abc_title_sep))
shift = (ax1 + abc_title_sep) - tx0
if shift > 0:
title_obj.set_x(base_x + shift)
elif base_x <= ax0 - abc_title_sep:
max_width = 2 * ((ax0 - abc_title_sep) - base_x)
shift = (ax0 - abc_title_sep) - tx1
if shift < 0:
title_obj.set_x(base_x + shift)
if 0 < max_width < title_bbox.width:
scale = max_width / title_bbox.width
title_obj.set_fontsize(title_obj.get_fontsize() * scale)
Expand Down
19 changes: 11 additions & 8 deletions ultraplot/tests/test_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -342,16 +342,18 @@ def test_title_manual_size_ignores_auto_shrink():
assert title_obj.get_fontsize() == 20


def test_title_shrinks_when_abc_overlaps_different_loc():
def test_title_shifts_when_abc_overlaps_different_loc():
"""
Ensure long titles shrink when overlapping abc at a different location.
Ensure centered titles keep their requested size by shifting away from abc.
"""
fig, axs = uplt.subplots(figsize=(3, 2))
axs.format(abc=True, title="X" * 200, titleloc="center", abcloc="left")
axs.format(abc=True, title="X" * 100, titleloc="center", abcloc="left")
title_obj = axs[0]._title_dict["center"]
original_size = title_obj.get_fontsize()
original_x = title_obj.get_position()[0]
fig.canvas.draw()
assert title_obj.get_fontsize() < original_size
assert title_obj.get_fontsize() == original_size
assert title_obj.get_position()[0] > original_x


def test_title_shrinks_right_aligned_same_location():
Expand Down Expand Up @@ -418,17 +420,18 @@ def test_title_no_shrink_when_no_overlap():
assert title_obj.get_fontsize() == original_size


def test_title_shrinks_centered_left_of_abc():
def test_title_shifts_centered_left_of_abc():
"""
Test that centered titles shrink when they are to the left of abc label.
This covers the specific case where base_x <= ax0 - pad for centered titles.
Centered titles should also shift left to avoid a right-side abc label.
"""
fig, axs = uplt.subplots(figsize=(3, 2))
axs.format(abc=True, title="X" * 100, titleloc="center", abcloc="right")
title_obj = axs[0]._title_dict["center"]
original_size = title_obj.get_fontsize()
original_x = title_obj.get_position()[0]
fig.canvas.draw()
assert title_obj.get_fontsize() < original_size
assert title_obj.get_fontsize() == original_size
assert title_obj.get_position()[0] < original_x
ticks = axs[0].get_xticks()
assert ticks.size > 0
xy = np.column_stack([ticks, np.zeros_like(ticks)])
Expand Down
Loading