Skip to content

Support escaped characters in matplotlibrc via double-quoted strings #19954

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

Closed
wants to merge 1 commit into from
Closed
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
3 changes: 3 additions & 0 deletions lib/matplotlib/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@
to MATLAB®, a registered trademark of The MathWorks, Inc.
"""

import ast
import atexit
from collections import namedtuple
from collections.abc import MutableMapping
Expand Down Expand Up @@ -753,6 +754,8 @@ def _rc_params_in_file(fname, transform=lambda x: x, fail_on_error=False):
key, val = tup
key = key.strip()
val = val.strip()
if len(val) >= 2 and val[0] == val[-1] == '"':
val = ast.literal_eval(val)
if key in rc_temp:
_log.warning('Duplicate key in file %r, line %d (%r)',
fname, line_no, line.rstrip('\n'))
Expand Down
7 changes: 7 additions & 0 deletions lib/matplotlib/mpl-data/matplotlibrc
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@
## All lines start with an additional '#', so that removing all leading '#'s
## yields a valid style file.
##
## Strings can be specified without qoutes, in which case they are interpreted
## literally, e.g.
## text.latex.preamble: \newcommand{\foo}{bar}
##
## If enclosed in double quotes, the string content is unescaped, e.g.
## date.autoformatter.hour: "%b %d\n%H:%M" # includes a newline
##
## Colors: for the color values below, you can either use
## - a Matplotlib color string, such as r, k, or b
## - an RGB tuple, such as (1.0, 0.5, 0.0)
Expand Down
10 changes: 9 additions & 1 deletion lib/matplotlib/tests/test_rcparams.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,12 @@ def test_rcparams(tmpdir):
linewidth = mpl.rcParams['lines.linewidth']

rcpath = Path(tmpdir) / 'test_rcparams.rc'
rcpath.write_text('lines.linewidth: 33')
rcpath.write_text(r"""
lines.linewidth: 33
text.latex.preamble: \newcommand{\foo}{\bar} # unquoted: no unescaping
date.autoformatter.hour: "%b %d\n%H:%M" # quoted: unescape to newline
date.autoformatter.year: "## %Y ##" # '#' in string
""")

# test context given dictionary
with mpl.rc_context(rc={'text.usetex': not usetex}):
Expand Down Expand Up @@ -66,6 +71,9 @@ def func():
# test rc_file
mpl.rc_file(rcpath)
assert mpl.rcParams['lines.linewidth'] == 33
assert mpl.rcParams['text.latex.preamble'] == r"\newcommand{\foo}{\bar}"
assert mpl.rcParams['date.autoformatter.hour'] == "%b %d\n%H:%M"
assert mpl.rcParams['date.autoformatter.year'] == "## %Y ##"


def test_RcParams_class():
Expand Down