8000 Fix passing Path to ps backend when text.usetex rc is True. by anntzer · Pull Request #13925 · matplotlib/matplotlib · GitHub
[go: up one dir, main page]

Skip to content

Fix passing Path to ps backend when text.usetex rc is True. #13925

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
Apr 14, 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
10 changes: 6 additions & 4 deletions lib/matplotlib/backends/backend_ps.py
Original file line number Diff line number Diff line change
Expand Up @@ -1119,12 +1119,14 @@ def _print_figure_tex(
the key 'Creator' is used.
"""
isEPSF = format == 'eps'
if isinstance(outfile, str):
title = outfile
elif is_writable_file_like(outfile):
if is_writable_file_like(outfile):
title = None
else:
raise ValueError("outfile must be a path or a file-like object")
try:
title = os.fspath(outfile)
except TypeError:
raise ValueError(
"outfile must be a path or a file-like object")

self.figure.dpi = 72 # ignore the dpi kwarg
width, height = self.figure.get_size_inches()
Expand Down
25 changes: 10 additions & 15 deletions lib/matplotlib/tests/test_backend_ps.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,12 @@ def test_savefig_to_stringio(format, use_log, rcParams):


def test_patheffects():
with mpl.rc_context():
mpl.rcParams['path.effects'] = [
patheffects.withStroke(linewidth=4, foreground='w')]
fig, ax = plt.subplots()
ax.plot([1, 2, 3])
with io.BytesIO() as ps:
fig.savefig(ps, format='ps')
mpl.rcParams['path.effects'] = [
patheffects.withStroke(linewidth=4, foreground='w')]
fig, ax = plt.subplots()
ax.plot([1, 2, 3])
with io.BytesIO() as ps:
fig.savefig(ps, format='ps')


@needs_usetex
Expand All @@ -86,18 +85,17 @@ def test_tilde_in_tempfilename(tmpdir):
# Tilde ~ in the tempdir path (e.g. TMPDIR, TMP or TEMP on windows
# when the username is very long and windows uses a short name) breaks
# latex before https://github.com/matplotlib/matplotlib/pull/5928
base_tempdir = Path(str(tmpdir), "short-1")
base_tempdir = Path(tmpdir, "short-1")
base_tempdir.mkdir()
# Change the path for new tempdirs, which is used internally by the ps
# backend to write a file.
with cbook._setattr_cm(tempfile, tempdir=str(base_tempdir)):
# usetex results in the latex call, which does not like the ~
plt.rc('text', usetex=True)
mpl.rcParams['text.usetex'] = True
plt.plot([1, 2, 3, 4])
plt.xlabel(r'\textbf{time} (s)')
output_eps = os.path.join(str(base_tempdir), 'tex_demo.eps')
# use the PS backend to write the file...
plt.savefig(output_eps, format="ps")
plt.savefig(base_tempdir / 'tex_demo.eps', format="ps")


def test_source_date_epoch():
Expand Down Expand Up @@ -133,11 +131,8 @@ def test_transparency():
@needs_usetex
def test_failing_latex(tmpdir):
"""Test failing latex subprocess call"""
path = str(tmpdir.join("tmpoutput.ps"))

mpl.rcParams['text.usetex'] = True

# This fails with "Double subscript"
plt.xlabel("$22_2_2$")
with pytest.raises(RuntimeError):
plt.savefig(path)
plt.savefig(Path(tmpdir, "tmpoutput.ps"))
0