Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 2fe4080

Browse files
meeseeksmachinetimhoffm
authored andcommitted
Backport PR #12164: Fix Annotation.contains. (#12194)
1 parent c7e9ca1 commit 2fe4080

File tree

1 file changed

+41
-33
lines changed

1 file changed

+41
-33
lines changed

lib/matplotlib/text.py

Lines changed: 41 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -890,25 +890,26 @@ def get_verticalalignment(self):
890890
return self._verticalalignment
891891

892892
def get_window_extent(self, renderer=None, dpi=None):
893-
'''
894-
Return a `~matplotlib.transforms.Bbox` object bounding
895-
the text, in display units.
893+
"""
894+
Return the `Bbox` bounding the text, in display units.
896895
897-
In addition to being used internally, this is useful for
898-
specifying clickable regions in a png file on a web page.
896+
In addition to being used internally, this is useful for specifying
897+
clickable regions in a png file on a web page.
899898
900-
*renderer* defaults to the _renderer attribute of the text
901-
object. This is not assigned until the first execution of
902-
:meth:`draw`, so you must use this kwarg if you want
903-
to call `.get_window_extent` prior to the first `draw`. For
904-
getting web page regions, it is simpler to call the method after
905-
saving the figure.
906-
907-
*dpi* defaults to self.figure.dpi; the renderer dpi is
908-
irrelevant. For the web application, if figure.dpi is not
909-
the value used when saving the figure, then the value that
910-
was used must be specified as the *dpi* argument.
911-
'''
899+
Parameters
900+
----------
901+
renderer : Renderer, optional
902+
A renderer is needed to compute the bounding box. If the artist
903+
has already been drawn, the renderer is cached; thus, it is only
904+
necessary to pass this argument when calling `get_window_extent`
905+
before the first `draw`. In practice, it is usually easier to
906+
trigger a draw first (e.g. by saving the figure).
907+
908+
dpi : float, optional
909+
The dpi value for computing the bbox, defaults to
910+
``self.figure.dpi`` (*not* the renderer dpi); should be set e.g. if
911+
to match regions with a figure saved with a custom dpi value.
912+
"""
912913
#return _unit_box
913914
if not self.get_visible():
914915
return Bbox.unit()
@@ -2343,29 +2344,36 @@ def draw(self, renderer):
23432344
Text.draw(self, renderer)
23442345

23452346
def get_window_extent(self, renderer=None):
2346-
'''
2347-
Return a :class:`~matplotlib.transforms.Bbox` object bounding
2348-
the text and arrow annotation, in display units.
2347+
"""
2348+
Return the `Bbox` bounding the text and arrow, in display units.
23492349
2350-
*renderer* defaults to the _renderer attribute of the text
2351-
object. This is not assigned until the first execution of
2352-
:meth:`draw`, so you must use this kwarg if you want
2353-
to call :meth:`get_window_extent` prior to the first
2354-
:meth:`draw`. For getting web page regions, it is
2355-
simpler to call the method after saving the figure. The
2356-
*dpi* used defaults to self.figure.dpi; the renderer dpi is
2357-
irrelevant.
2358-
'''
2359-
self.update_positions(renderer)
2350+
Parameters
2351+
----------
2352+
renderer : Renderer, optional
2353+
A renderer is needed to compute the bounding box. If the artist
2354+
has already been drawn, the renderer is cached; thus, it is only
2355+
necessary to pass this argument when calling `get_window_extent`
2356+
before the first `draw`. In practice, it is usually easier to
2357+
trigger a draw first (e.g. by saving the figure).
2358+
"""
2359+
# This block is the same as in Text.get_window_extent, but we need to
2360+
# set the renderer before calling update_positions().
23602361
if not self.get_visible():
23612362
return Bbox.unit()
2363+
if renderer is not None:
2364+
self._renderer = renderer
2365+
if self._renderer is None:
2366+
self._renderer = self.figure._cachedRenderer
2367+
if self._renderer is None:
2368+
raise RuntimeError('Cannot get window extent w/o renderer')
2369+
2370+
self.update_positions(self._renderer)
23622371

2363-
text_bbox = Text.get_window_extent(self, renderer=renderer)
2372+
text_bbox = Text.get_window_extent(self)
23642373
bboxes = [text_bbox]
23652374

23662375
if self.arrow_patch is not None:
2367-
bboxes.append(
2368-
self.arrow_patch.get_window_extent(renderer=renderer))
2376+
bboxes.append(self.arrow_patch.get_window_extent())
23692377

23702378
return Bbox.union(bboxes)
23712379

0 commit comments

Comments
 (0)