Thanks to visit codestin.com
Credit goes to github.com

Skip to content

savefig with bbox_inches='tight' takes account of all text artists #689

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions lib/matplotlib/axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -8250,6 +8250,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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it really worth creating the extra variable? Why not just write

return [t for t in self.texts if t.get_visible()]

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I often like the extra named variable before a return so when I encounter problems down the road I can %debug and get in there and explore more efficiently.



def get_tightbbox(self, renderer, call_axes_locator=True):
"""
return the tight bounding box of the axes.
Expand Down
8 changes: 6 additions & 2 deletions lib/matplotlib/backend_bases.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

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])

Expand Down
7 changes: 7 additions & 0 deletions lib/matplotlib/figure.py
Original file line number Diff line number Diff line change
Expand Up @@ -1288,6 +1288,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):
"""
Expand Down