Skip to content

Commit 98cfadf

Browse files
ENH: Allow tupple for borderpad in AnchoredOffsetbox
1 parent 53af231 commit 98cfadf

File tree

2 files changed

+47
-7
lines changed

2 files changed

+47
-7
lines changed

lib/matplotlib/offsetbox.py

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -946,8 +946,10 @@ 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
950-
Padding between the offsetbox frame and the *bbox_to_anchor*.
949+
borderpad : float or tuple[float, float], default: 0.5
950+
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.
951953
child : `.OffsetBox`
952954
The box that will be anchored.
953955
prop : `.FontProperties`
@@ -1052,14 +1054,29 @@ def set_bbox_to_anchor(self, bbox, transform=None):
10521054
self.stale = True
10531055

10541056
@_compat_get_offset
1057+
10551058
def get_offset(self, bbox, renderer):
1056-
# docstring inherited
1057-
pad = (self.borderpad
1058-
* renderer.points_to_pixels(self.prop.get_size_in_points()))
1059+
fontsize_in_pixels = renderer.points_to_pixels(self.prop.get_size_in_points())
1060+
try:
1061+
borderpad_x, borderpad_y = self.borderpad
1062+
except (TypeError, ValueError):
1063+
borderpad_x = self.borderpad
1064+
borderpad_y = self.borderpad
1065+
pad_x_pixels = borderpad_x * fontsize_in_pixels
1066+
pad_y_pixels = borderpad_y * fontsize_in_pixels
10591067
bbox_to_anchor = self.get_bbox_to_anchor()
1068+
padded_bbox_to_anchor = Bbox.from_extents(
1069+
bbox_to_anchor.x0 + pad_x_pixels,
1070+
bbox_to_anchor.y0 + pad_y_pixels,
1071+
bbox_to_anchor.x1 - pad_x_pixels,
1072+
bbox_to_anchor.y1 - pad_y_pixels
1073+
)
10601074
x0, y0 = _get_anchored_bbox(
1061-
self.loc, Bbox.from_bounds(0, 0, bbox.width, bbox.height),
1062-
bbox_to_anchor, pad)
1075+
self.loc,
1076+
Bbox.from_bounds(0, 0, bbox.width, bbox.height),
1077+
padded_bbox_to_anchor,
1078+
0
1079+
)
10631080
return x0 - bbox.x0, y0 - bbox.y0
10641081

10651082
def update_frame(self, bbox, fontsize=None):

lib/matplotlib/tests/test_offsetbox.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -470,3 +470,26 @@ def test_draggable_in_subfigure():
470470
bbox = ann.get_window_extent()
471471
MouseEvent("button_press_event", fig.canvas, bbox.x1+2, bbox.y1+2)._process()
472472
assert not ann._draggable.got_artist
473+
474+
475+
def test_anchored_offsetbox_tuple_borderpad_direct():
476+
"""Test AnchoredOffsetbox directly with a tuple borderpad."""
477+
from matplotlib.offsetbox import AnchoredText
478+
479+
fig, ax = plt.subplots()
480+
481+
# Baseline with small padding
482+
text1 = AnchoredText("test1", loc='lower left', borderpad=(1, 1))
483+
ax.add_artist(text1)
484+
fig.canvas.draw()
485+
pos1 = text1.get_window_extent()
486+
487+
# Your feature with larger padding
488+
text2 = AnchoredText("test2", loc='lower left', borderpad=(10, 10))
489+
ax.add_artist(text2)
490+
fig.canvas.draw()
491+
pos2 = text2.get_window_extent()
492+
493+
# Assert that the position has moved up and to the right
494+
assert pos2.x0 > pos1.x0
495+
assert pos2.y0 > pos1.y0

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy