Skip to content

Commit b5eba12

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 b5eba12

File tree

3 files changed

+16
-2
lines changed

3 files changed

+16
-2
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

0 commit comments

Comments
 (0)