From 3a48f5e95b3c14806a48e8c52c624e24b826cc99 Mon Sep 17 00:00:00 2001 From: cvanelteren Date: Fri, 1 May 2026 08:55:42 +1000 Subject: [PATCH] Fix scaling of title --- ultraplot/axes/base.py | 12 ++++++++++-- ultraplot/tests/test_axes.py | 19 +++++++++++-------- 2 files changed, 21 insertions(+), 10 deletions(-) diff --git a/ultraplot/axes/base.py b/ultraplot/axes/base.py index 43253ea65..416b33430 100644 --- a/ultraplot/axes/base.py +++ b/ultraplot/axes/base.py @@ -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) diff --git a/ultraplot/tests/test_axes.py b/ultraplot/tests/test_axes.py index 2c432b515..c90474142 100644 --- a/ultraplot/tests/test_axes.py +++ b/ultraplot/tests/test_axes.py @@ -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(): @@ -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)])