From 016f2453bd08dba759fc5ba08d4283bacd77a0f7 Mon Sep 17 00:00:00 2001 From: Jae-Joon Lee Date: Mon, 30 Jan 2012 13:36:36 +0900 Subject: [PATCH 1/3] savefig with bbox_inches='tight' takes account of all text artists --- lib/matplotlib/axes.py | 5 +++++ lib/matplotlib/backend_bases.py | 8 ++++++-- lib/matplotlib/figure.py | 7 +++++++ 3 files changed, 18 insertions(+), 2 deletions(-) diff --git a/lib/matplotlib/axes.py b/lib/matplotlib/axes.py index e4957f57aeab..a819179d6914 100644 --- a/lib/matplotlib/axes.py +++ b/lib/matplotlib/axes.py @@ -8267,6 +8267,11 @@ def matshow(self, Z, **kwargs): integer=True)) return im + def get_default_bbox_extra_artists(self, renderer): + bbox_extra_artists = [t for t in self.texts if t.get_visible()] + 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..ded10756b276 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(renderer) + + 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..b4abe2c738f5 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, renderer): + 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(renderer)) + return bbox_extra_artists + def get_tightbbox(self, renderer): """ From d8e4b95fcca613d585071835fb8849b131ed6389 Mon Sep 17 00:00:00 2001 From: Jae-Joon Lee Date: Tue, 31 Jan 2012 11:39:35 +0900 Subject: [PATCH 2/3] include legends as default_bbox_extra_artists --- lib/matplotlib/axes.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/matplotlib/axes.py b/lib/matplotlib/axes.py index a819179d6914..1fd5203fea80 100644 --- a/lib/matplotlib/axes.py +++ b/lib/matplotlib/axes.py @@ -8269,6 +8269,8 @@ def matshow(self, Z, **kwargs): def get_default_bbox_extra_artists(self, renderer): 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 From b00d883e52ccaeb8cfd2821cc039996e431631c1 Mon Sep 17 00:00:00 2001 From: Jae-Joon Lee Date: Tue, 31 Jan 2012 11:43:08 +0900 Subject: [PATCH 3/3] remove renderer parameter from get_default_bbox_extra_artists methods --- lib/matplotlib/axes.py | 2 +- lib/matplotlib/backend_bases.py | 2 +- lib/matplotlib/figure.py | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/matplotlib/axes.py b/lib/matplotlib/axes.py index 1fd5203fea80..2e04c73a0c97 100644 --- a/lib/matplotlib/axes.py +++ b/lib/matplotlib/axes.py @@ -8267,7 +8267,7 @@ def matshow(self, Z, **kwargs): integer=True)) return im - def get_default_bbox_extra_artists(self, renderer): + 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_) diff --git a/lib/matplotlib/backend_bases.py b/lib/matplotlib/backend_bases.py index ded10756b276..dc9f1ff54078 100644 --- a/lib/matplotlib/backend_bases.py +++ b/lib/matplotlib/backend_bases.py @@ -1986,7 +1986,7 @@ def print_figure(self, filename, dpi=None, facecolor='w', edgecolor='w', 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(renderer) + bbox_extra_artists = self.figure.get_default_bbox_extra_artists() bb = [a.get_window_extent(renderer) for a in bbox_extra_artists] diff --git a/lib/matplotlib/figure.py b/lib/matplotlib/figure.py index b4abe2c738f5..3487db28b8ef 100644 --- a/lib/matplotlib/figure.py +++ b/lib/matplotlib/figure.py @@ -1287,11 +1287,11 @@ def waitforbuttonpress(self, timeout=-1): return blocking_input(timeout=timeout) - def get_default_bbox_extra_artists(self, renderer): + 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(renderer)) + bbox_extra_artists.extend(ax.get_default_bbox_extra_artists()) return bbox_extra_artists