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

Skip to content

Commit 1e79e72

Browse files
committed
Support escaped characters in matplotlibrc via double-quoted strings
1 parent 0151110 commit 1e79e72

File tree

3 files changed

+17
-1
lines changed

3 files changed

+17
-1
lines changed

lib/matplotlib/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@
8181
to MATLAB®, a registered trademark of The MathWorks, Inc.
8282
"""
8383

84+
import ast
8485
import atexit
8586
from collections import namedtuple
8687
from collections.abc import MutableMapping
@@ -753,6 +754,8 @@ def _rc_params_in_file(fname, transform=lambda x: x, fail_on_error=False):
753754
key, val = tup
754755
key = key.strip()
755756
val = val.strip()
757+
if len(val) >= 2 and val[0] == val[-1] == '"':
758+
val = ast.literal_eval(val)
756759
if key in rc_temp:
757760
_log.warning('Duplicate key in file %r, line %d (%r)',
758761
fname, line_no, line.rstrip('\n'))

lib/matplotlib/mpl-data/matplotlibrc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,13 @@
3030
## All lines start with an additional '#', so that removing all leading '#'s
3131
## yields a valid style file.
3232
##
33+
## Strings can be specified without qoutes, in which case they are interpreted
34+
## literally, e.g.
35+
## text.latex.preamble: \newcommand{\foo}{bar}
36+
##
37+
## If enclosed in double quotes, the string content is unescaped, e.g.
38+
## date.autoformatter.hour: "%b %d\n%H:%M" # includes a newline
39+
##
3340
## Colors: for the color values below, you can either use
3441
## - a Matplotlib color string, such as r, k, or b
3542
## - an RGB tuple, such as (1.0, 0.5, 0.0)

lib/matplotlib/tests/test_rcparams.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,11 @@ def test_rcparams(tmpdir):
3838
linewidth = mpl.rcParams['lines.linewidth']
3939

4040
rcpath = Path(tmpdir) / 'test_rcparams.rc'
41-
rcpath.write_text('lines.linewidth: 33')
41+
rcpath.write_text(r"""
42+
lines.linewidth: 33
43+
text.latex.preamble: \newcommand{\foo}{\bar} # unquoted: no unescaping
44+
date.autoformatter.hour: "%b %d\n%H:%M" # quoted: unescape to newline
45+
""")
4246

4347
# test context given dictionary
4448
with mpl.rc_context(rc={'text.usetex': not usetex}):
@@ -66,6 +70,8 @@ def func():
6670
# test rc_file
6771
mpl.rc_file(rcpath)
6872
assert mpl.rcParams['lines.linewidth'] == 33
73+
assert mpl.rcParams['text.latex.preamble'] == r"\newcommand{\foo}{\bar}"
74+
assert mpl.rcParams['date.autoformatter.hour'] == "%b %d\n%H:%M"
6975

7076

7177
def test_RcParams_class():

0 commit comments

Comments
 (0)