Skip to content

Commit d3cc9d7

Browse files
authored
Merge pull request #22589 from timhoffm/quoted-matplotlibrc
Support quoted strings in matplotlibrc
2 parents 921e9ac + 7c378a8 commit d3cc9d7

File tree

5 files changed

+54
-6
lines changed

5 files changed

+54
-6
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
Double-quoted strings in matplotlibrc
2+
-------------------------------------
3+
4+
You can now use double-quotes around strings. This allows using the '#'
5+
character in strings. Without quotes, '#' is interpreted as start of a comment.
6+
In particular, you can now define hex-colors:
7+
8+
.. code-block:: none
9+
10+
grid.color: "#b0b0b0"

lib/matplotlib/__init__.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -780,7 +780,7 @@ def _rc_params_in_file(fname, transform=lambda x: x, fail_on_error=False):
780780
try:
781781
for line_no, line in enumerate(fd, 1):
782782
line = transform(line)
783-
strippedline = line.split('#', 1)[0].strip()
783+
strippedline = cbook._strip_comment(line)
784784
if not strippedline:
785785
continue
786786
tup = strippedline.split(':', 1)
@@ -791,6 +791,8 @@ def _rc_params_in_file(fname, transform=lambda x: x, fail_on_error=False):
791791
key, val = tup
792792
key = key.strip()
793793
val = val.strip()
794+
if val.startswith('"') and val.endswith('"'):
795+
val = val[1:-1] # strip double quotes
794796
if key in rc_temp:
795797
_log.warning('Duplicate key in file %r, line %d (%r)',
796798
fname, line_no, line.rstrip('\n'))

lib/matplotlib/cbook/__init__.py

+15
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,21 @@ def strip_math(s):
400400
return s
401401

402402

403+
def _strip_comment(s):
404+
"""Strip everything from the first unquoted #."""
405+
pos = 0
406+
while True:
407+
quote_pos = s.find('"', pos)
408+
hash_pos = s.find('#', pos)
409+
if quote_pos < 0:
410+
without_comment = s if hash_pos < 0 else s[:hash_pos]
411+
return without_comment.strip()
412+
elif 0 <= hash_pos < quote_pos:
413+
return s[:hash_pos].strip()
414+
else:
415+
pos = s.find('"', quote_pos + 1) + 1 # behind closing quote
416+
417+
403418
def is_writable_file_like(obj):
404419
"""Return whether *obj* looks like a file object with a *write* method."""
405420
return callable(getattr(obj, 'write', None))

lib/matplotlib/mpl-data/matplotlibrc

+10-5
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,15 @@
3333
## Colors: for the color values below, you can either use
3434
## - a Matplotlib color string, such as r, k, or b
3535
## - an RGB tuple, such as (1.0, 0.5, 0.0)
36-
## - a hex string, such as ff00ff
36+
## - a double-quoted hex string, such as "#ff00ff".
37+
## The unquoted string ff00ff is also supported for backward
38+
## compatibility, but is discouraged.
3739
## - a scalar grayscale intensity such as 0.75
3840
## - a legal html color name, e.g., red, blue, darkslategray
3941
##
42+
## String values may optionally be enclosed in double quotes, which allows
43+
## using the comment character # in the string.
44+
##
4045
## Matplotlib configuration are currently divided into following parts:
4146
## - BACKENDS
4247
## - LINES
@@ -506,10 +511,10 @@
506511
## ***************************************************************************
507512
## * GRIDS *
508513
## ***************************************************************************
509-
#grid.color: b0b0b0 # grid color
510-
#grid.linestyle: - # solid
511-
#grid.linewidth: 0.8 # in points
512-
#grid.alpha: 1.0 # transparency, between 0.0 and 1.0
514+
#grid.color: "#b0b0b0" # grid color
515+
#grid.linestyle: - # solid
516+
#grid.linewidth: 0.8 # in points
517+
#grid.alpha: 1.0 # transparency, between 0.0 and 1.0
513518

514519

515520
## ***************************************************************************

lib/matplotlib/tests/test_cbook.py

+16
Original file line numberDiff line numberDiff line change
@@ -408,6 +408,22 @@ def test_func2():
408408
cb.process("test2")
409409

410410

411+
@pytest.mark.parametrize('line, result', [
412+
('a : no_comment', 'a : no_comment'),
413+
('a : "quoted str"', 'a : "quoted str"'),
414+
('a : "quoted str" # comment', 'a : "quoted str"'),
415+
('a : "#000000"', 'a : "#000000"'),
416+
('a : "#000000" # comment', 'a : "#000000"'),
417+
('a : ["#000000", "#FFFFFF"]', 'a : ["#000000", "#FFFFFF"]'),
418+
('a : ["#000000", "#FFFFFF"] # comment', 'a : ["#000000", "#FFFFFF"]'),
419+
('a : val # a comment "with quotes"', 'a : val'),
420+
('# only comment "with quotes" xx', ''),
421+
])
422+
def test_strip_comment(line, result):
423+
"""Strip everything from the first unqouted #."""
424+
assert cbook._strip_comment(line) == result
425+
426+
411427
def test_sanitize_sequence():
412428
d = {'a': 1, 'b': 2, 'c': 3}
413429
k = ['a', 'b', 'c']

0 commit comments

Comments
 (0)