@@ -263,11 +263,19 @@ def set_offset(self, xy):
263
263
xy : (float, float) or callable
264
264
The (x, y) coordinates of the offset in display units. These can
265
265
either be given explicitly as a tuple (x, y), or by providing a
266
- function that converts the extent into the offset. This function
267
- must have the signature::
266
+ function that dynamically computes an offset (taking the arguments
267
+ passed to `.OffsetBox.get_offset`). It is recommended to make such
268
+ functions take no arguments.
269
+
270
+ Before version 3.6, the callable had to have the signature::
268
271
269
272
def offset(width, height, xdescent, ydescent, renderer) \
270
273
-> (float, float)
274
+
275
+ For backwards compatibility, callables with arbitrary signatures
276
+ are currently accepted as long as compatible arguments are
277
+ passed in calls to `.set_offset`. This should be considered an
278
+ implementation detail, and may be deprecated in the future.
271
279
"""
272
280
self ._offset = xy
273
281
self .stale = True
@@ -276,9 +284,12 @@ def get_offset(self, *args, **kwargs):
276
284
"""
277
285
Return the (x, y) offset.
278
286
279
- Parameters must be passed if the offset is dynamically determined by a
280
- callable (see `~.OffsetBox.set_offset`), and are forwarded to that
281
- callable.
287
+ Parameters are usually not necessary. The only exception can occur
288
+ if you have defined a callable to calculate the offset dynamically (see
289
+ `~.OffsetBox.set_offset`). It is now recommended that such a
290
+ callable does not take parameters. However, for backward-compatibility,
291
+ callables with parameters are still supported; these parameters must be
292
+ provided to `.get_offset` so that we can pass them on.
282
293
"""
283
294
return (self ._offset (* args , ** kwargs ) if callable (self ._offset )
284
295
else self ._offset )
@@ -340,7 +351,9 @@ def get_extent(self, renderer):
340
351
def get_window_extent (self , renderer ):
341
352
# docstring inherited
342
353
w , h , xd , yd = self .get_extent (renderer )
343
- px , py = self .get_offset (w , h , xd , yd , renderer )
354
+ # dynamic offset compute callables may need to access the renderer.
355
+ self ._cached_renderer = renderer
356
+ px , py = self .get_offset ()
344
357
return mtransforms .Bbox .from_bounds (px - xd , py - yd , w , h )
345
358
346
359
def draw (self , renderer ):
@@ -349,7 +362,7 @@ def draw(self, renderer):
349
362
to the given *renderer*.
350
363
"""
351
364
w , h , xdescent , ydescent , offsets = self .get_extent_offsets (renderer )
352
- px , py = self .get_offset (w , h , xdescent , ydescent , renderer )
365
+ px , py = self .get_offset ()
353
366
for c , (ox , oy ) in zip (self .get_visible_children (), offsets ):
354
367
c .set_offset ((px + ox , py + oy ))
355
368
c .draw (renderer )
@@ -530,7 +543,7 @@ def get_extent_offsets(self, renderer):
530
543
def draw (self , renderer ):
531
544
# docstring inherited
532
545
w , h , xdescent , ydescent , offsets = self .get_extent_offsets (renderer )
533
- px , py = self .get_offset (w , h , xdescent , ydescent , renderer )
546
+ px , py = self .get_offset ()
534
547
for c , (ox , oy ) in zip (self .get_visible_children (), offsets ):
535
548
c .set_offset ((px + ox , py + oy ))
536
549
@@ -1036,9 +1049,10 @@ def get_window_extent(self, renderer):
1036
1049
# docstring inherited
1037
1050
# Update the offset func, which depends on the dpi of the renderer
1038
1051
# (because of the padding).
1052
+ w , h , xd , yd = self .get_extent (renderer )
1039
1053
fontsize = renderer .points_to_pixels (self .prop .get_size_in_points ())
1040
1054
1041
- def _offset (w , h , xd , yd , renderer ):
1055
+ def _offset (* args , ** kwargs ): # args are ignored; left for backcompat.
1042
1056
bbox = Bbox .from_bounds (0 , 0 , w , h )
1043
1057
pad = self .borderpad * fontsize
1044
1058
bbox_to_anchor = self .get_bbox_to_anchor ()
@@ -1064,9 +1078,7 @@ def draw(self, renderer):
1064
1078
self .update_frame (bbox , fontsize )
1065
1079
self .patch .draw (renderer )
1066
1080
1067
- width , height , xdescent , ydescent = self .get_extent (renderer )
1068
-
1069
- px , py = self .get_offset (width , height , xdescent , ydescent , renderer )
1081
+ px , py = self .get_offset ()
1070
1082
1071
1083
self .get_child ().set_offset ((px , py ))
1072
1084
self .get_child ().draw (renderer )
@@ -1545,8 +1557,7 @@ def __init__(self, ref_artist, offsetbox, use_blit=False):
1545
1557
def save_offset (self ):
1546
1558
offsetbox = self .offsetbox
1547
1559
renderer = offsetbox .figure ._cachedRenderer
1548
- w , h , xd , yd = offsetbox .get_extent (renderer )
1549
- offset = offsetbox .get_offset (w , h , xd , yd , renderer )
1560
+ offset = offsetbox .get_offset ()
1550
1561
self .offsetbox_x , self .offsetbox_y = offset
1551
1562
self .offsetbox .set_offset (offset )
1552
1563
0 commit comments