8000 Refactor tightbbox into Artist · matplotlib/matplotlib@970842a · GitHub
[go: up one dir, main page]

Skip to content

Commit 970842a

Browse files
committed
Refactor tightbbox into Artist
1 parent d17a994 commit 970842a

File tree

3 files changed

+31
-22
lines changed

3 files changed

+31
-22
lines changed

lib/matplotlib/artist.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,34 @@ def get_window_extent(self, renderer):
256256
"""
257257
return Bbox([[0, 0], [0, 0]])
258258

259+
def get_tightbbox(self, renderer):
260+
"""
261+
Like `Artist.get_window_extent`, but includes any clipping.
262+
263+
Parameters
264+
----------
265+
renderer : RendererBase instance
266+
renderer that will be used to draw the figures (i.e.
267+
``fig.canvas.get_renderer()``)
268+
269+
Returns
270+
-------
271+
`BboxBase` : containing the bounding box (in figure-relative
272+
co-ordinates).
273+
"""
274+
275+
bbox = self.get_window_extent(renderer)
276+
if self.get_clip_on():
277+
clip_box = self.get_clip_box()
278+
if clip_box is not None:
279+
bbox = Bbox.intersection(bbox, clip_box)
280+
clip_path = self.get_clip_path()
281+
if clip_path is not None and bbox is not None:
282+
clip_path = clip_path.get_fully_transformed_path()
283+
bbox = Bbox.intersection(bbox,
284+
clip_path.get_extents())
285+
return bbox
286+
259287
def add_callback(self, func):
260288
"""
261289
Adds a callback function that will be called whenever one of

lib/matplotlib/axes/_base.py

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4167,18 +4167,8 @@ def get_tightbbox(self, renderer, call_axes_locator=True,
41674167
if isinstance(a, Legend) and a.get_visible():
41684168
bb.append(a._legend_box.get_window_extent(renderer))
41694169
else:
4170-
bbox = a.get_window_extent(renderer)
4171-
if a.get_clip_on():
4172-
clip_box = a.get_clip_box()
4173-
if clip_box is not None:
4174-
bbox = mtransforms.Bbox.intersection(bbox, clip_box)
4175-
clip_path = a.get_clip_path()
4176-
if clip_path is not None and bbox is not None:
4177-
clip_path = clip_path.get_fully_transformed_path()
4178-
bbox = mtransforms.Bbox.intersection(bbox,
4179-
clip_path.get_extents())
4180-
if bbox is not None and (bbox.width != 0 or
4181-
bbox.height != 0):
4170+
bbox = a.get_tightbbox(renderer)
4171+
if bbox is not None and (bbox.width != 0 or bbox.height != 0):
41824172
bb.append(bbox)
41834173

41844174
_bbox = mtransforms.Bbox.union(

lib/matplotlib/figure.py

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2030,16 +2030,7 @@ def get_tightbbox(self, renderer, bbox_extra_artists=None):
20302030
if isinstance(a, mlegend.Legend) and a.get_visible():
20312031
bb.append(a._legend_box.get_window_extent(renderer))
20322032
else:
2033-
bbox = a.get_window_extent(renderer)
2034-
if a.get_clip_on():
2035-
clip_box = a.get_clip_box()
2036-
if clip_box is not None:
2037-
bbox = Bbox.intersection(bbox, clip_box)
2038-
clip_path = a.get_clip_path()
2039-
if clip_path is not None and bbox is not None:
2040-
clip_path = clip_path.get_fully_transformed_path()
2041-
bbox = Bbox.intersection(bbox,
2042-
clip_path.get_extents())
2033+
bbox = a.get_tightbbox(renderer)
20432034
if bbox is not None and (bbox.width != 0 or
20442035
bbox.height != 0):
20452036
bb.append(bbox)

0 commit comments

Comments
 (0)
0