Skip to content

Fix pgf+bbox_inches=tight combo #16732

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
40 changes: 13 additions & 27 deletions lib/matplotlib/backend_bases.py
Original file line number Diff line number Diff line change
Expand Up @@ -1516,27 +1516,6 @@ def __init__(self, name, canvas, key, x=0, y=0, guiEvent=None):
self.key = key


def _get_renderer(figure, print_method):
"""
Get the renderer that would be used to save a `~.Figure`, and cache it on
the figure.
"""
# This is implemented by triggering a draw, then immediately jumping out of
# Figure.draw() by raising an exception.

class Done(Exception):
pass

def _draw(renderer): raise Done(renderer)

with cbook._setattr_cm(figure, draw=_draw):
try:
print_method(io.BytesIO())
except Done as exc:
figure._cachedRenderer, = exc.args
return figure._cachedRenderer


def _is_non_interactive_terminal_ipython(ip):
"""
Return whether we are in a a terminal IPython, but non interactive.
Expand Down Expand Up @@ -2059,12 +2038,20 @@ def print_figure(self, filename, dpi=None, facecolor=None, edgecolor=None,
bbox_inches = rcParams['savefig.bbox']

if bbox_inches:
# call adjust_bbox to save only the given area
if bbox_inches == "tight":
renderer = _get_renderer(
self.figure,
functools.partial(
print_method, dpi=dpi, orientation=orientation))
self.figure.draw(renderer)
# When bbox_inches == "tight", it saves the figure twice.
# The first save command (to a BytesIO) is just to estimate
# the bounding box of the figure.
result = print_method(
io.BytesIO(),
dpi=dpi,
facecolor=facecolor,
edgecolor=edgecolor,
orientation=orientation,
dryrun=True,
**kwargs)
renderer = self.figure._cachedRenderer
bbox_artists = kwargs.pop("bbox_extra_artists", None)
bbox_inches = self.figure.get_tightbbox(renderer,
bbox_extra_artists=bbox_artists)
Expand All @@ -2074,7 +2061,6 @@ def print_figure(self, filename, dpi=None, facecolor=None, edgecolor=None,

bbox_inches = bbox_inches.padded(pad)

# call adjust_bbox to save only the given area
restore_bbox = tight_bbox.adjust_bbox(self.figure, bbox_inches,
canvas.fixed_dpi)

Expand Down
2 changes: 0 additions & 2 deletions lib/matplotlib/backends/backend_agg.py
Original file line number Diff line number Diff line change
Expand Up @@ -548,7 +548,6 @@ def print_to_buffer(self):
# print_figure(), and the latter ensures that `self.figure.dpi` already
# matches the dpi kwarg (if any).

@cbook._delete_parameter("3.2", "dryrun")
def print_jpg(self, filename_or_obj, *args, dryrun=False,
pil_kwargs=None, **kwargs):
"""
Expand Down Expand Up @@ -601,7 +600,6 @@ def print_jpg(self, filename_or_obj, *args, dryrun=False,

print_jpeg = print_jpg

@cbook._delete_parameter("3.2", "dryrun")
def print_tif(self, filename_or_obj, *args, dryrun=False,
pil_kwargs=None, **kwargs):
FigureCanvasAgg.draw(self)
Expand Down
1 change: 0 additions & 1 deletion lib/matplotlib/backends/backend_pgf.py
Original file line number Diff line number Diff line change
Expand Up @@ -810,7 +810,6 @@ class FigureCanvasPgf(FigureCanvasBase):
def get_default_filetype(self):
return 'pdf'

@cbook._delete_parameter("3.2", "dryrun")
def _print_pgf_to_fh(self, fh, *args,
dryrun=False, bbox_inches_restore=None, **kwargs):
if dryrun:
Expand Down
1 change: 0 additions & 1 deletion lib/matplotlib/backends/backend_ps.py
Original file line number Diff line number Diff line change
Expand Up @@ -851,7 +851,6 @@ def _print_ps(self, outfile, format, *args,
printer(outfile, format, dpi, facecolor, edgecolor,
orientation, papertype, **kwargs)

@cbook._delete_parameter("3.2", "dryrun")
def _print_figure(
self, outfile, format, dpi, facecolor, edgecolor,
orientation, papertype, *,
Expand Down
1 change: 0 additions & 1 deletion lib/matplotlib/table.py
Original file line number Diff line number Diff line change
Expand Up @@ -455,7 +455,6 @@ def get_children(self):

def get_window_extent(self, renderer):
"""Return the bounding box of the table in window coords."""
self._update_positions(renderer)
boxes = [cell.get_window_extent(renderer)
for cell in self._cells.values()]
return Bbox.union(boxes)
Expand Down
8 changes: 8 additions & 0 deletions lib/matplotlib/tests/test_backend_pgf.py
Original file line number Diff line number Diff line change
Expand Up @@ -282,3 +282,11 @@ def test_tex_restart_after_error():
fig = plt.figure() # start from scratch
fig.suptitle(r"this is ok")
fig.savefig(BytesIO(), format="pgf")


@needs_xelatex
def test_bbox_inches_tight(tmpdir):
fig, ax = plt.subplots()
ax.imshow([[0, 1], [2, 3]])
fig.savefig(os.path.join(tmpdir, "test.pdf"), backend="pgf",
bbox_inches="tight")