@@ -946,8 +946,13 @@ def __init__(self, loc, *,
946946 See the parameter *loc* of `.Legend` for details.
947947 pad : float, default: 0.4
948948 Padding around the child as fraction of the fontsize.
949- borderpad : float, default: 0.5
949+ borderpad : float or (float, float) , default: 0.5
950950 Padding between the offsetbox frame and the *bbox_to_anchor*.
951+ If a float, the same padding is used for both x and y.
952+ If a tuple of two floats, it specifies the (x, y) padding.
953+
954+ .. versionadded:: 3.11
955+ The *borderpad* parameter now accepts a tuple of (x, y) paddings.
951956 child : `.OffsetBox`
952957 The box that will be anchored.
953958 prop : `.FontProperties`
@@ -1054,12 +1059,22 @@ def set_bbox_to_anchor(self, bbox, transform=None):
10541059 @_compat_get_offset
10551060 def get_offset (self , bbox , renderer ):
10561061 # docstring inherited
1057- pad = (self .borderpad
1058- * renderer .points_to_pixels (self .prop .get_size_in_points ()))
1062+ fontsize_in_pixels = renderer .points_to_pixels (self .prop .get_size_in_points ())
1063+ try :
1064+ borderpad_x , borderpad_y = self .borderpad
1065+ except TypeError :
1066+ borderpad_x = self .borderpad
1067+ borderpad_y = self .borderpad
1068+ pad_x_pixels = borderpad_x * fontsize_in_pixels
1069+ pad_y_pixels = borderpad_y * fontsize_in_pixels
10591070 bbox_to_anchor = self .get_bbox_to_anchor ()
10601071 x0 , y0 = _get_anchored_bbox (
1061- self .loc , Bbox .from_bounds (0 , 0 , bbox .width , bbox .height ),
1062- bbox_to_anchor , pad )
1072+ self .loc ,
1073+ Bbox .from_bounds (0 , 0 , bbox .width , bbox .height ),
1074+ bbox_to_anchor ,
1075+ pad_x_pixels ,
1076+ pad_y_pixels
1077+ )
10631078 return x0 - bbox .x0 , y0 - bbox .y0
10641079
10651080 def update_frame (self , bbox , fontsize = None ):
@@ -1084,15 +1099,15 @@ def draw(self, renderer):
10841099 self .stale = False
10851100
10861101
1087- def _get_anchored_bbox (loc , bbox , parentbbox , borderpad ):
1102+ def _get_anchored_bbox (loc , bbox , parentbbox , pad_x , pad_y ):
10881103 """
10891104 Return the (x, y) position of the *bbox* anchored at the *parentbbox* with
1090- the *loc* code with the *borderpad*.
1105+ the *loc* code with the *borderpad* and padding *pad_x*, *pad_y* .
10911106 """
10921107 # This is only called internally and *loc* should already have been
10931108 # validated. If 0 (None), we just let ``bbox.anchored`` raise.
10941109 c = [None , "NE" , "NW" , "SW" , "SE" , "E" , "W" , "E" , "S" , "N" , "C" ][loc ]
1095- container = parentbbox .padded (- borderpad )
1110+ container = parentbbox .padded (- pad_x , - pad_y )
10961111 return bbox .anchored (c , container = container ).p0
10971112
10981113
0 commit comments