From 27737e1406486258fb8b13fae13bacbb0ebb4c4b Mon Sep 17 00:00:00 2001 From: Antony Lee Date: Wed, 11 Mar 2020 17:18:09 +0100 Subject: [PATCH 1/3] Revert "Walk the artist tree when preparing for saving with tight bbox." This reverts commit 7961de383f7fda33f5b320aba1268213c57224bb. --- lib/matplotlib/backend_bases.py | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/matplotlib/backend_bases.py b/lib/matplotlib/backend_bases.py index 0c85673b2b10..cd6e4e9517de 100644 --- a/lib/matplotlib/backend_bases.py +++ b/lib/matplotlib/backend_bases.py @@ -2064,7 +2064,6 @@ def print_figure(self, filename, dpi=None, facecolor=None, edgecolor=None, self.figure, functools.partial( print_method, dpi=dpi, orientation=orientation)) - self.figure.draw(renderer) bbox_artists = kwargs.pop("bbox_extra_artists", None) bbox_inches = self.figure.get_tightbbox(renderer, bbox_extra_artists=bbox_artists) From df608ff25b282524ecab40ffcb80a49de8b0198a Mon Sep 17 00:00:00 2001 From: Antony Lee Date: Wed, 11 Mar 2020 17:18:51 +0100 Subject: [PATCH 2/3] Revert "Deprecate the dryrun parameter to print_foo()." This reverts commit 71db23e6863031414cda1f105a75f6ebb1e735bf. --- lib/matplotlib/backend_bases.py | 39 +++++++++----------------- lib/matplotlib/backends/backend_agg.py | 2 -- lib/matplotlib/backends/backend_pgf.py | 1 - lib/matplotlib/backends/backend_ps.py | 1 - lib/matplotlib/table.py | 1 - 5 files changed, 13 insertions(+), 31 deletions(-) diff --git a/lib/matplotlib/backend_bases.py b/lib/matplotlib/backend_bases.py index cd6e4e9517de..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,11 +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)) + # 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) @@ -2073,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) From 4496fd0615582b644cfa9c22f7a5dabff60d295d Mon Sep 17 00:00:00 2001 From: Antony Lee Date: Wed, 11 Mar 2020 17:24:02 +0100 Subject: [PATCH 3/3] Test tight-pgf combo. --- lib/matplotlib/tests/test_backend_pgf.py | 8 ++++++++ 1 file changed, 8 insertions(+) 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") pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

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:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy