From 82b97d31f82e1f59a0e90f2e563e1f8ef02fc77a 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 3f418a30aed6..1582c190318d 100644 --- a/lib/matplotlib/axes.py +++ b/lib/matplotlib/axes.py @@ -8250,6 +8250,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 be4ee71ef089..99b5bc1c34fc 100644 --- a/lib/matplotlib/backend_bases.py +++ b/lib/matplotlib/backend_bases.py @@ -1994,8 +1994,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 87d819c1260e..7db0f15eff92 100644 --- a/lib/matplotlib/figure.py +++ b/lib/matplotlib/figure.py @@ -1288,6 +1288,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 565efe4dabd070729653da3c1248be6af44738e4 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 1582c190318d..dad405c85541 100644 --- a/lib/matplotlib/axes.py +++ b/lib/matplotlib/axes.py @@ -8252,6 +8252,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 5e2a0c448f63bdc0214cd678ea460aa0baf8dacb 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 dad405c85541..1db655d01656 100644 --- a/lib/matplotlib/axes.py +++ b/lib/matplotlib/axes.py @@ -8250,7 +8250,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 99b5bc1c34fc..7336297cfb6d 100644 --- a/lib/matplotlib/backend_bases.py +++ b/lib/matplotlib/backend_bases.py @@ -1996,7 +1996,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 7db0f15eff92..9183d345fa93 100644 --- a/lib/matplotlib/figure.py +++ b/lib/matplotlib/figure.py @@ -1288,11 +1288,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