diff --git a/lib/matplotlib/axes.py b/lib/matplotlib/axes.py index e4957f57aeab..2e04c73a0c97 100644 --- a/lib/matplotlib/axes.py +++ b/lib/matplotlib/axes.py @@ -8267,6 +8267,13 @@ def matshow(self, Z, **kwargs): integer=True)) return im + def get_default_bbox_extra_artists(self): + bbox_extra_artists = [t for t in self.texts if t.get_visible()] + if self.legend_: + bbox_extra_artists.append(self.legend_) + return bbox_extra_artists + + def get_tightbbox(self, renderer, call_axes_locator=True): """ return the tight bounding box of the axes. diff --git a/lib/matplotlib/backend_bases.py b/lib/matplotlib/backend_bases.py index d7c93cbe34ac..dc9f1ff54078 100644 --- a/lib/matplotlib/backend_bases.py +++ b/lib/matplotlib/backend_bases.py @@ -1984,8 +1984,12 @@ def print_figure(self, filename, dpi=None, facecolor='w', edgecolor='w', renderer = self.figure._cachedRenderer bbox_inches = self.figure.get_tightbbox(renderer) - bb = [a.get_window_extent(renderer) for a \ - in kwargs.pop("bbox_extra_artists", [])] + bbox_extra_artists = kwargs.pop("bbox_extra_artists", None) + if bbox_extra_artists is None: + bbox_extra_artists = self.figure.get_default_bbox_extra_artists() + + bb = [a.get_window_extent(renderer) for a in bbox_extra_artists] + if bb: _bbox = Bbox.union([b for b in bb if b.width!=0 or b.height!=0]) diff --git a/lib/matplotlib/figure.py b/lib/matplotlib/figure.py index dd4f181fada6..3487db28b8ef 100644 --- a/lib/matplotlib/figure.py +++ b/lib/matplotlib/figure.py @@ -1287,6 +1287,13 @@ def waitforbuttonpress(self, timeout=-1): return blocking_input(timeout=timeout) + def get_default_bbox_extra_artists(self): + bbox_extra_artists = [t for t in self.texts if t.get_visible()] + for ax in self.axes: + if ax.get_visible(): + bbox_extra_artists.extend(ax.get_default_bbox_extra_artists()) + return bbox_extra_artists + def get_tightbbox(self, renderer): """