diff --git a/lib/matplotlib/backend_bases.py b/lib/matplotlib/backend_bases.py index 0c85673b2b10..fe19620943ab 100644 --- a/lib/matplotlib/backend_bases.py +++ b/lib/matplotlib/backend_bases.py @@ -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. @@ -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) @@ -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) diff --git a/lib/matplotlib/backends/backend_agg.py b/lib/matplotlib/backends/backend_agg.py index 67e80611ef10..de24d0c6ccee 100644 --- a/lib/matplotlib/backends/backend_agg.py +++ b/lib/matplotlib/backends/backend_agg.py @@ -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): """ @@ -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) diff --git a/lib/matplotlib/backends/backend_pgf.py b/lib/matplotlib/backends/backend_pgf.py index 6c8e7e06c8b3..54318abc730c 100644 --- a/lib/matplotlib/backends/backend_pgf.py +++ b/lib/matplotlib/backends/backend_pgf.py @@ -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: diff --git a/lib/matplotlib/backends/backend_ps.py b/lib/matplotlib/backends/backend_ps.py index 0eee04f07cf6..7562de37d97f 100644 --- a/lib/matplotlib/backends/backend_ps.py +++ b/lib/matplotlib/backends/backend_ps.py @@ -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, *, diff --git a/lib/matplotlib/table.py b/lib/matplotlib/table.py index c4faafdea5fd..69d26d998a91 100644 --- a/lib/matplotlib/table.py +++ b/lib/matplotlib/table.py @@ -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) diff --git a/lib/matplotlib/tests/test_backend_pgf.py b/lib/matplotlib/tests/test_backend_pgf.py index 269606c7feca..7843b4101041 100644 --- a/lib/matplotlib/tests/test_backend_pgf.py +++ b/lib/matplotlib/tests/test_backend_pgf.py @@ -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")
Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.
Alternative Proxies: