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

Skip to content

Commit 05ce6c5

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 05ce6c5

File tree

7 files changed

+55
-41
lines changed

7 files changed

+55
-41
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 12th 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")``.

examples/color/color_demo.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@
1515
5) a X11/CSS4 ("html") color name, e.g. ``"blue"``;
1616
6) a name from the `xkcd color survey <https://xkcd.com/color/rgb/>`__,
1717
prefixed with ``'xkcd:'`` (e.g., ``'xkcd:sky blue'``);
18-
7) a "Cn" color spec, i.e. `'C'` followed by a single digit, which is an index
19-
into the default property cycle
20-
(``matplotlib.rcParams['axes.prop_cycle']``); the indexing occurs at artist
21-
creation time and defaults to black if the cycle does not include color.
18+
7) a "Cn" color spec, i.e. `'C'` followed by a number, which is an index into
19+
the default property cycle (``matplotlib.rcParams['axes.prop_cycle']``); the
20+
indexing occurs at artist creation time and defaults to black if the cycle
21+
does not include color.
2222
8) one of ``{'tab:blue', 'tab:orange', 'tab:green',
2323
'tab:red', 'tab:purple', 'tab:brown', 'tab:pink',
2424
'tab:gray', 'tab:olive', 'tab:cyan'}`` which are the Tableau Colors from the

lib/matplotlib/axes/_axes.py

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1517,33 +1517,12 @@ def plot(self, *args, scalex=True, scaley=True, **kwargs):
15171517
15181518
A format string consists of a part for color, marker and line::
15191519
1520-
fmt = '[color][marker][line]'
1520+
fmt = '[marker][line][color]'
15211521
15221522
Each of them is optional. If not provided, the value from the style
15231523
cycle is used. Exception: If ``line`` is given, but no ``marker``,
15241524
the data will be a line without markers.
15251525
1526-
**Colors**
1527-
1528-
The following color abbreviations are supported:
1529-
1530-
============= ===============================
1531-
character color
1532-
============= ===============================
1533-
``'b'`` blue
1534-
``'g'`` green
1535-
``'r'`` red
1536-
``'c'`` cyan
1537-
``'m'`` magenta
1538-
``'y'`` yellow
1539-
``'k'`` black
1540-
``'w'`` white
1541-
============= ===============================
1542-
1543-
If the color is the only part of the format string, you can
1544-
additionally use any `matplotlib.colors` spec, e.g. full names
1545-
(``'green'``) or hex strings (``'#008000'``).
1546-
15471526
**Markers**
15481527
15491528
============= ===============================
@@ -1587,11 +1566,33 @@ def plot(self, *args, scalex=True, scaley=True, **kwargs):
15871566
Example format strings::
15881567
15891568
'b' # blue markers with default shape
1590-
'ro' # red circles
1591-
'g-' # green solid line
1569+
'or' # red circles
1570+
'-g' # green solid line
15921571
'--' # dashed line with default color
1593-
'k^:' # black triangle_up markers connected by a dotted line
1572< 67E6 /td>+
'^k:' # black triangle_up markers connected by a dotted line
15941573
1574+
**Colors**
1575+
1576+
The supported color abbreviations are the single letter codes
1577+
1578+
============= ===============================
1579+
character color
1580+
============= ===============================
1581+
``'b'`` blue
1582+
``'g'`` green
1583+
``'r'`` red
1584+
``'c'`` cyan
1585+
``'m'`` magenta
1586+
``'y'`` yellow
1587+
``'k'`` black
1588+
``'w'`` white
1589+
============= ===============================
1590+
1591+
and the ``'CN'`` colors that index into the default property cycle.
1592+
1593+
If the color is the only part of the format string, you can
1594+
additionally use any `matplotlib.colors` spec, e.g. full names
1595+
(``'green'``) or hex strings (``'#008000'``).
15951596
"""
15961597
lines = []
15971598

lib/matplotlib/colors.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,10 @@
4949
'tab:red', 'tab:purple', 'tab:brown', 'tab:pink',
5050
'tab:gray', 'tab:olive', 'tab:cyan'}`` which are the Tableau Colors from the
5151
'T10' categorical palette (which is the default color cycle);
52-
* a "CN" color spec, i.e. `'C'` followed by a single digit, which is an index
53-
into the default property cycle (``matplotlib.rcParams['axes.prop_cycle']``);
54-
the indexing occurs at artist creation time and defaults to black if the
55-
cycle does not include color.
52+
* a "CN" color spec, i.e. `'C'` followed by a number, which is an index into
53+
the default property cycle (``matplotlib.rcParams['axes.prop_cycle']``); the
54+
indexing occurs at artist creation time and defaults to black if the cycle
55+
does not include color.
5656
5757
All string specifications of color, other than "CN", are case-insensitive.
5858
@@ -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+
('C12', 'C12'), # 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

tutorials/colors/colors.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@
1818
'tab:red', 'tab:purple', 'tab:brown', 'tab:pink',
1919
'tab:gray', 'tab:olive', 'tab:cyan'}`` which are the Tableau Colors from the
2020
'T10' categorical palette (which is the default color cycle);
21-
* a "CN" color spec, i.e. `'C'` followed by a single digit, which is an index
22-
into the default property cycle (``matplotlib.rcParams['axes.prop_cycle']``);
23-
the indexing occurs at artist creation time and defaults to black if the
24-
cycle does not include color.
21+
* a "CN" color spec, i.e. `'C'` followed by a number, which is an index into
22+
the default property cycle (``matplotlib.rcParams['axes.prop_cycle']``); the
23+
indexing occurs at artist creation time and defaults to black if the cycle
24+
does not include color.
2525
2626
"Red", "Green" and "Blue", are the intensities of those colors, the combination
2727
of which span the colorspace.

0 commit comments

Comments
 (0)
0