Skip to content

Commit ce32dd2

Browse files
committed
Merge pull request matplotlib#739 from leejjoon/tight-bbox-all-texts-v1.1.x
savefig with bbox_inches='tight' takes account of all text artists (against v1.1.x)
2 parents 389670a + b00d883 commit ce32dd2

File tree

3 files changed

+20
-2
lines changed

3 files changed

+20
-2
lines changed

lib/matplotlib/axes.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8267,6 +8267,13 @@ def matshow(self, Z, **kwargs):
82678267
integer=True))
82688268
return im
82698269

8270+
def get_default_bbox_extra_artists(self):
8271+
bbox_extra_artists = [t for t in self.texts if t.get_visible()]
8272+
if self.legend_:
8273+
bbox_extra_artists.append(self.legend_)
8274+
return bbox_extra_artists
8275+
8276+
82708277
def get_tightbbox(self, renderer, call_axes_locator=True):
82718278
"""
82728279
return the tight bounding box of the axes.

lib/matplotlib/backend_bases.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1984,8 +1984,12 @@ def print_figure(self, filename, dpi=None, facecolor='w', edgecolor='w',
19841984
renderer = self.figure._cachedRenderer
19851985
bbox_inches = self.figure.get_tightbbox(renderer)
19861986

1987-
bb = [a.get_window_extent(renderer) for a \
1988-
in kwargs.pop("bbox_extra_artists", [])]
1987+
bbox_extra_artists = kwargs.pop("bbox_extra_artists", None)
1988+
if bbox_extra_artists is None:
1989+
bbox_extra_artists = self.figure.get_default_bbox_extra_artists()
1990+
1991+
bb = [a.get_window_extent(renderer) for a in bbox_extra_artists]
1992+
19891993
if bb:
19901994
_bbox = Bbox.union([b for b in bb if b.width!=0 or b.height!=0])
19911995

lib/matplotlib/figure.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1287,6 +1287,13 @@ def waitforbuttonpress(self, timeout=-1):
12871287
return blocking_input(timeout=timeout)
12881288

12891289

1290+
def get_default_bbox_extra_artists(self):
1291+
bbox_extra_artists = [t for t in self.texts if t.get_visible()]
1292+
for ax in self.axes:
1293+
if ax.get_visible():
1294+
bbox_extra_artists.extend(ax.get_default_bbox_extra_artists())
1295+
return bbox_extra_artists
1296+
12901297

12911298
def get_tightbbox(self, renderer):
12921299
"""

0 commit comments

Comments
 (0)