8000 Support Cn colors with n>=10. · matplotlib/matplotlib@53d828f · GitHub
[go: up one dir, main page]

Skip to content

Commit 53d828f

Browse files
committed
Support Cn colors with n>=10.
While the change is mildly backwards-incompatible (as noted in the API changes), Cn syntax is by far the simplest way to navigate the color-cycle, so I think the change is worth it.
1 parent a43fd85 commit 53d828f

File tree

4 files changed

+17
-4
lines changed

4 files changed

+17
-4
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
``Cn`` colors now support ``n>=10``
2+
```````````````````````````````````
3+
4+
It is now possible to go beyond the tenth color in the property cycle using
5+
``Cn`` syntax, e.g. ``plt.plot([1, 2], color="C11")`` now uses the 11th color
6+
in the cycle.
7+
8+
Note that previously, a construct such as ``plt.plot([1, 2], "C11")`` would be
9+
interpreted as a request to use color ``C1`` and marker ``1`` (an "inverted Y").
10+
To obtain such a plot, one should now use ``plt.plot([1, 2], "1C1")`` (so that
11+
the first "1" gets correctly interpreted as a marker specification), or, more
12+
explicitly, ``plt.plot([1, 2], marker="1", color="C1")``.

lib/matplotlib/colors.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ def _sanitize_extrema(ex):
115115

116116
def _is_nth_color(c):
117117
"""Return whether *c* can be interpreted as an item in the color cycle."""
118-
return isinstance(c, str) and re.match(r"\AC[0-9]\Z", c)
118+
return isinstance(c, str) and re.match(r"\AC[0-9]+\Z", c)
119119

120120

121121
def is_color_like(c):
@@ -169,7 +169,7 @@ def to_rgba(c, alpha=None):
169169
from matplotlib import rcParams
170170
prop_cycler = rcParams['axes.prop_cycle']
171171
colors = prop_cycler.by_key().get('color', ['k'])
172-
c = colors[int(c[1]) % len(colors)]
172+
c = colors[int(c[1:]) % len(colors)]
173173
try:
174174
rgba = _colors_full_map.cache[c, alpha]
175175
except (KeyError, TypeError): # Not in cache, or unhashable.

lib/matplotlib/tests/test_colors.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -637,6 +637,8 @@ def test_cn():
637637
['xkcd:blue', 'r'])
638638
assert mcolors.to_hex("C0") == '#0343df'
639639
assert mcolors.to_hex("C1") == '#ff0000'
640+
assert mcolors.to_hex("C10") == '#0343df'
641+
assert mcolors.to_hex("C11") == '#ff0000'
640642

641643
matplotlib.rcParams['axes.prop_cycle'] = cycler('color', ['8e4585', 'r'])
642644

lib/matplotlib/tests/test_rcparams.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,7 @@ def generate_validator_testcases(valid):
307307
('AABBCC', '#AABBCC'), # RGB hex code
308308
('AABBCC00', '#AABBCC00'), # RGBA hex code
309309
('tab:blue', 'tab:blue'), # named color
310-
('C0', 'C0'), # color from cycle
310+
('C123', 'C123'), # color from cycle
311311
('(0, 1, 0)', [0.0, 1.0, 0.0]), # RGB tuple
312312
((0, 1, 0), (0, 1, 0)), # non-string version
313313
('(0, 1, 0, 1)', [0.0, 1.0, 0.0, 1.0]), # RGBA tuple
@@ -316,7 +316,6 @@ def generate_validator_testcases(valid):
316316

317317
),
318318
'fail': (('tab:veryblue', ValueError), # invalid name
319-
('C123', ValueError), # invalid RGB(A) code and cycle index
320319
('(0, 1)', ValueError), # tuple with length < 3
321320
('(0, 1, 0, 1, 0)', ValueError), # tuple with length > 4
322321
('(0, 1, none)', ValueError), # cannot cast none to float

0 commit comments

Comments
 (0)
0