Skip to content

Deprecate the dryrun parameter to print_foo(). #14134

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

Merged
merged 1 commit into from
May 7, 2019
Merged
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
4 changes: 4 additions & 0 deletions doc/api/next_api_changes/2019-05-05-AL.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Deprecations
````````````

The ``dryrun`` parameter to the various ``FigureCanvasFoo.print_foo`` methods is deprecated.
39 changes: 26 additions & 13 deletions lib/matplotlib/backend_bases.py
Original file line number Diff line number Diff line change
Expand Up @@ -1528,6 +1528,27 @@ 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


class FigureCanvasBase(object):
"""
The canvas the figure renders into.
Expand Down Expand Up @@ -2038,20 +2059,11 @@ 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":
# 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
renderer = _get_renderer(
self.figure,
functools.partial(
print_method, dpi=dpi, orientation=orientation))
bbox_artists = kwargs.pop("bbox_extra_artists", None)
bbox_inches = self.figure.get_tightbbox(renderer,
bbox_extra_artists=bbox_artists)
Expand All @@ -2061,6 +2073,7 @@ 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: 2 additions & 0 deletions lib/matplotlib/backends/backend_agg.py
Original file line number Diff line number Diff line change
Expand Up @@ -544,6 +544,7 @@ 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 @@ -598,6 +599,7 @@ 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):
buf, size = self.print_to_buffer()
Expand Down
1 change: 1 addition & 0 deletions lib/matplotlib/backends/backend_pgf.py
Original file line number Diff line number Diff line change
Expand Up @@ -775,6 +775,7 @@ 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: 1 addition & 0 deletions lib/matplotlib/backends/backend_ps.py
Original file line number Diff line number Diff line change
Expand Up @@ -870,6 +870,7 @@ def _print_ps(self, outfile, format, *args,
orientation, isLandscape, papertype,
**kwargs)

@cbook._delete_parameter("3.2", "dryrun")
def _print_figure(
self, outfile, format, dpi=72, facecolor='w', edgecolor='w',
orientation='portrait', isLandscape=False, papertype=None,
Expand Down
1 change: 1 addition & 0 deletions lib/matplotlib/table.py
Original file line number Diff line number Diff line change
Expand Up @@ -458,6 +458,7 @@ def get_children(self):

def get_window_extent(self, renderer):
"""Return the bounding box of the table in window coords."""
self._update_positions(renderer)
Copy link
Member

Choose a reason for hiding this comment

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

Could you add a quick test for the issue solved by this change?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

A few tests in test_bbox_tight fail without it (they test tight-bboxing tables).

boxes = [cell.get_window_extent(renderer)
for cell in self._cells.values()]
return Bbox.union(boxes)
Expand Down