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

Skip to content

Deprecate papersize=auto in PostScript #25785

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 2 commits into from
Aug 9, 2023
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
6 changes: 6 additions & 0 deletions doc/api/next_api_changes/deprecations/25784-ES.rst
Copy link
Member

Choose a reason for hiding this comment

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

This should have been 25785 I just noted...

Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Automatic papersize selection in PostScript
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Setting :rc:`ps.papersize` to ``'auto'`` or passing ``papersize='auto'`` to
`.Figure.savefig` is deprecated. Either pass an explicit paper type name, or
omit this parameter to use the default from the rcParam.
31 changes: 20 additions & 11 deletions lib/matplotlib/backends/backend_ps.py
Original file line number Diff line number Diff line change
Expand Up @@ -867,18 +867,24 @@ def _print_figure(
# find the appropriate papertype
width, height = self.figure.get_size_inches()
if papertype == 'auto':
papertype = _get_papertype(
*orientation.swap_if_landscape((width, height)))
paper_width, paper_height = orientation.swap_if_landscape(
papersize[papertype])
_api.warn_deprecated("3.8", name="papertype='auto'",
addendum="Pass an explicit paper type, or omit the "
"*papertype* argument entirely.")
papertype = _get_papertype(*orientation.swap_if_landscape((width, height)))

if mpl.rcParams['ps.usedistiller']:
# distillers improperly clip eps files if pagesize is too small
if width > paper_width or height > paper_height:
papertype = _get_papertype(
*orientation.swap_if_landscape((width, height)))
paper_width, paper_height = orientation.swap_if_landscape(
papersize[papertype])
if is_eps:
paper_width, paper_height = width, height
else:
paper_width, paper_height = orientation.swap_if_landscape(
papersize[papertype])

if mpl.rcParams['ps.usedistiller']:
# distillers improperly clip eps files if pagesize is too small
Copy link
Member

Choose a reason for hiding this comment

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

This comment seems incongruous with this being in the else block of if is_eps:

If this code is specifically targeted to fix eps, then it would have to be in the if block, and if it is just wrong, perhaps it could be taken out entirely.

It also relies on the _get_papertype function which the point of removing auto is to remove that function because it is problematic in implementation.

Copy link
Member Author

Choose a reason for hiding this comment

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

Ah good point, since as the first commit notes, the distiller ignores the paper type and asks the distiller to crop, this is probably something that could be dropped. But I guess I will have to test out a few combinations to be sure.

if width > paper_width or height > paper_height:
papertype = _get_papertype(
*orientation.swap_if_landscape((width, height)))
paper_width, paper_height = orientation.swap_if_landscape(
papersize[papertype])

# center the figure on the paper
xo = 72 * 0.5 * (paper_width - width)
Expand Down Expand Up @@ -1055,6 +1061,9 @@ def _print_figure_tex(
self.figure.get_size_inches())
else:
if papertype == 'auto':
_api.warn_deprecated("3.8", name="papertype='auto'",
addendum="Pass an explicit paper type, or "
"omit the *papertype* argument entirely.")
papertype = _get_papertype(width, height)
paper_width, paper_height = papersize[papertype]

Expand Down
17 changes: 14 additions & 3 deletions lib/matplotlib/rcsetup.py
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,19 @@ def validate_ps_distiller(s):
return ValidateInStrings('ps.usedistiller', ['ghostscript', 'xpdf'])(s)


def _validate_papersize(s):
# Re-inline this validator when the 'auto' deprecation expires.
s = ValidateInStrings("ps.papersize",
["auto", "letter", "legal", "ledger",
*[f"{ab}{i}" for ab in "ab" for i in range(11)]],
ignorecase=True)(s)
if s == "auto":
_api.warn_deprecated("3.8", name="ps.papersize='auto'",
addendum="Pass an explicit paper type, or omit the "
"*ps.papersize* rcParam entirely.")
return s


# A validator dedicated to the named line styles, based on the items in
# ls_mapper, and a list of possible strings read from Line2D.set_linestyle
_validate_named_linestyle = ValidateInStrings(
Expand Down Expand Up @@ -1180,9 +1193,7 @@ def _convert_validator_spec(key, conv):
"tk.window_focus": validate_bool, # Maintain shell focus for TkAgg

# Set the papersize/type
"ps.papersize": _ignorecase(["auto", "letter", "legal", "ledger",
*[f"{ab}{i}"
for ab in "ab" for i in range(11)]]),
"ps.papersize": _validate_papersize,
"ps.useafm": validate_bool,
# use ghostscript or xpdf to distill ps output
"ps.usedistiller": validate_ps_distiller,
Expand Down
9 changes: 9 additions & 0 deletions lib/matplotlib/tests/test_backend_ps.py
Original file line number Diff line number Diff line change
Expand Up @@ -336,3 +336,12 @@ def test_colorbar_shift(tmp_path):
norm = mcolors.BoundaryNorm([-1, -0.5, 0.5, 1], cmap.N)
plt.scatter([0, 1], [1, 1], c=[0, 1], cmap=cmap, norm=norm)
plt.colorbar()


def test_auto_papersize_deprecation():
fig = plt.figure()
with pytest.warns(mpl.MatplotlibDeprecationWarning):
fig.savefig(io.BytesIO(), format='eps', papertype='auto')

with pytest.warns(mpl.MatplotlibDeprecationWarning):
mpl.rcParams['ps.papersize'] = 'auto'