Skip to content

Commit e76e2a7

Browse files
committed
Clarify padding variable names in _update_title_position
Renamed local variables to better reflect their purpose: - abcpad -> abc_title_sep_pts (abc-title separation in points) - pad -> abc_title_sep (abc-title separation in axes coords) - abc_pad -> abc_offset (user's horizontal offset in axes coords) Added comprehensive inline comments explaining: - The difference between abc-title separation (spacing when co-located) and abc offset (user's horizontal shift via abcpad parameter) - Unit conversions from points to axes coordinates - Source and purpose of each variable Updated documentation: - Enhanced abcpad parameter docstring to clarify it's a horizontal offset - Added inline comments to instance variables at initialization This addresses co-author feedback requesting clarification of the relationship between abcpad, self._abc_pad, pad, and abc_pad variables. No API changes - all modifications are internal refactoring only.
1 parent 1e0c960 commit e76e2a7

1 file changed

Lines changed: 38 additions & 26 deletions

File tree

ultraplot/axes/base.py

Lines changed: 38 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,9 @@
354354
inside the axes. This can help them stand out on top of artists plotted
355355
inside the axes.
356356
abcpad : float or unit-spec, default: :rc:`abc.pad`
357-
The padding for the inner and outer titles and a-b-c labels.
357+
Horizontal offset to shift the a-b-c label position. Positive values move
358+
the label right, negative values move it left. This is separate from
359+
`abctitlepad`, which controls spacing between abc and title when co-located.
358360
%(units.pt)s
359361
abc_kw, title_kw : dict-like, optional
360362
Additional settings used to update the a-b-c label and title
@@ -846,8 +848,10 @@ def __init__(self, *args, **kwargs):
846848
self._auto_format = None # manipulated by wrapper functions
847849
self._abc_border_kwargs = {}
848850
self._abc_loc = None
849-
self._abc_pad = 0
850-
self._abc_title_pad = rc["abc.titlepad"]
851+
self._abc_pad = 0 # User's horizontal offset for abc label (in points)
852+
self._abc_title_pad = rc[
853+
"abc.titlepad"
854+
] # Spacing between abc and title when co-located
851855
self._title_above = rc["title.above"]
852856
self._title_border_kwargs = {} # title border properties
853857
self._title_loc = None
@@ -3014,7 +3018,8 @@ def _update_title_position(self, renderer):
30143018
# This is known matplotlib problem but especially annoying with top panels.
30153019
# NOTE: See axis.get_ticks_position for inspiration
30163020
pad = self._title_pad
3017-
abcpad = self._abc_title_pad
3021+
# Horizontal separation between abc label and title when co-located (in points)
3022+
abc_title_sep_pts = self._abc_title_pad
30183023
if self.xaxis.get_visible() and any(
30193024
tick.tick2line.get_visible() and not tick.label2.get_visible()
30203025
for tick in self.xaxis.majorTicks
@@ -3042,13 +3047,19 @@ def _update_title_position(self, renderer):
30423047

30433048
# Offset title away from a-b-c label
30443049
# NOTE: Title texts all use axes transform in x-direction
3045-
3046-
# Offset title away from a-b-c label
3050+
# We need to convert padding values from points to axes coordinates (0-1 normalized)
30473051
atext, ttext = aobj.get_text(), tobj.get_text()
30483052
awidth = twidth = 0
30493053
width_inches = self._get_size_inches()[0]
3050-
pad = (abcpad / 72) / width_inches
3051-
abc_pad = (self._abc_pad / 72) / width_inches
3054+
3055+
# Convert abc-title separation from points to axes coordinates
3056+
# This is the spacing BETWEEN abc and title when they share the same location
3057+
abc_title_sep = (abc_title_sep_pts / 72) / width_inches
3058+
3059+
# Convert user's horizontal offset from points to axes coordinates
3060+
# This is the user-specified shift for the abc label position (via abcpad parameter)
3061+
abc_offset = (self._abc_pad / 72) / width_inches
3062+
30523063
ha = aobj.get_ha()
30533064

30543065
# Get dimensions of non-empty elements
@@ -3076,17 +3087,17 @@ def _update_title_position(self, renderer):
30763087
scale = 1
30773088
base_x = tobj.get_position()[0]
30783089
if ha == "left":
3079-
available = 1 - (base_x + awidth + pad)
3090+
available = 1 - (base_x + awidth + abc_title_sep)
30803091
if available < twidth and available > 0:
30813092
scale = available / twidth
30823093
elif ha == "right":
3083-
available = base_x + abc_pad - pad - awidth
3094+
available = base_x + abc_offset - abc_title_sep - awidth
30843095
if available < twidth and available > 0:
30853096
scale = available / twidth
30863097
elif ha == "center":
30873098
# Conservative fit for centered titles sharing the abc location
3088-
left_room = base_x - 0.5 * (awidth + pad)
3089-
right_room = 1 - (base_x + 0.5 * (awidth + pad))
3099+
left_room = base_x - 0.5 * (awidth + abc_title_sep)
3100+
right_room = 1 - (base_x + 0.5 * (awidth + abc_title_sep))
30903101
max_room = min(left_room, right_room)
30913102
if max_room < twidth / 2 and max_room > 0:
30923103
scale = (2 * max_room) / twidth
@@ -3099,19 +3110,20 @@ def _update_title_position(self, renderer):
30993110
aoffset = toffset = 0
31003111
if atext and ttext:
31013112
if ha == "left":
3102-
toffset = awidth + pad
3113+
toffset = awidth + abc_title_sep
31033114
elif ha == "right":
3104-
aoffset = -(twidth + pad)
3115+
aoffset = -(twidth + abc_title_sep)
31053116
elif ha == "center":
3106-
toffset = 0.5 * (awidth + pad)
3107-
aoffset = -0.5 * (twidth + pad)
3117+
toffset = 0.5 * (awidth + abc_title_sep)
3118+
aoffset = -0.5 * (twidth + abc_title_sep)
31083119

31093120
# Apply positioning adjustments
3121+
# For abc label: apply offset from co-located title + user's horizontal offset
31103122
if atext:
31113123
aobj.set_x(
31123124
aobj.get_position()[0]
31133125
+ aoffset
3114-
+ (self._abc_pad / 72) / (self._get_size_inches()[0])
3126+
+ abc_offset # User's horizontal shift (from abcpad parameter)
31153127
)
31163128
if ttext:
31173129
tobj.set_x(tobj.get_position()[0] + toffset)
@@ -3135,21 +3147,21 @@ def _update_title_position(self, renderer):
31353147
)
31363148
ax0, ax1 = abc_bbox.x0, abc_bbox.x1
31373149
tx0, tx1 = title_bbox.x0, title_bbox.x1
3138-
if tx0 < ax1 + pad and tx1 > ax0 - pad:
3150+
if tx0 < ax1 + abc_title_sep and tx1 > ax0 - abc_title_sep:
31393151
base_x = title_obj.get_position()[0]
31403152
ha = title_obj.get_ha()
31413153
max_width = 0
31423154
if ha == "left":
3143-
if base_x <= ax0 - pad:
3144-
max_width = (ax0 - pad) - base_x
3155+
if base_x <= ax0 - abc_title_sep:
3156+
max_width = (ax0 - abc_title_sep) - base_x
31453157
elif ha == "right":
3146-
if base_x >= ax1 + pad:
3147-
max_width = base_x - (ax1 + pad)
3158+
if base_x >= ax1 + abc_title_sep:
3159+
max_width = base_x - (ax1 + abc_title_sep)
31483160
elif ha == "center":
3149-
if base_x >= ax1 + pad:
3150-
max_width = 2 * (base_x - (ax1 + pad))
3151-
elif base_x <= ax0 - pad:
3152-
max_width = 2 * ((ax0 - pad) - base_x)
3161+
if base_x >= ax1 + abc_title_sep:
3162+
max_width = 2 * (base_x - (ax1 + abc_title_sep))
3163+
elif base_x <= ax0 - abc_title_sep:
3164+
max_width = 2 * ((ax0 - abc_title_sep) - base_x)
31533165
if 0 < max_width < title_bbox.width:
31543166
scale = max_width / title_bbox.width
31553167
title_obj.set_fontsize(title_obj.get_fontsize() * scale)

0 commit comments

Comments
 (0)