Skip to content

Commit 8ba38c3

Browse files
committed
Support Cn, n>9 in plot() shorthand format.
1 parent 6378d48 commit 8ba38c3

File tree

3 files changed

+25
-6
lines changed

3 files changed

+25
-6
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
plot() shorthand format interprets "Cn" (n>9) as a color-cycle color
2+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3+
Previously, ``plot(..., "-C11")`` would be interpreted as requesting a plot
4+
using linestyle "-", color "C1" (color #1 of the color cycle), and marker "1"
5+
("tri-down"). It is now interpreted as requesting linestyle "-" and color
6+
"C11" (color #11 of the color cycle).
7+
8+
It is recommended to pass ambiguous markers (such as "1") explicitly using the
9+
*marker* keyword argument. If the shorthand form is desired, such markers can
10+
also be unambiguously set by putting them *before* the color string.

lib/matplotlib/axes/_base.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import logging
66
from numbers import Real
77
from operator import attrgetter
8+
import re
89
import types
910

1011
import numpy as np
@@ -189,13 +190,14 @@ def _process_plot_format(fmt, *, ambiguous_fmt_datakey=False):
189190
raise ValueError(errfmt.format(fmt, "two color symbols"))
190191
color = c
191192
i += 1
192-
elif c == 'C' and i < len(fmt) - 1:
193-
color_cycle_number = int(fmt[i + 1])
194-
color = mcolors.to_rgba(f"C{color_cycle_number}")
195-
i += 2
193+
elif c == "C":
194+
cn_color = re.match(r"C\d+", fmt[i:])
195+
if not cn_color:
196+
raise ValueError(errfmt.format(fmt, "'C' must be followed by a number"))
197+
color = mcolors.to_rgba(cn_color[0])
198+
i += len(cn_color[0])
196199
else:
197-
raise ValueError(
198-
errfmt.format(fmt, f"unrecognized character {c!r}"))
200+
raise ValueError(errfmt.format(fmt, f"unrecognized character {c!r}"))
199201

200202
if linestyle is None and marker is None:
201203
linestyle = mpl.rcParams['lines.linestyle']

lib/matplotlib/tests/test_axes.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8511,6 +8511,8 @@ def test_empty_line_plots():
85118511
(":-", r"':-' is not a valid format string \(two linestyle symbols\)"),
85128512
("rk", r"'rk' is not a valid format string \(two color symbols\)"),
85138513
(":o-r", r"':o-r' is not a valid format string \(two linestyle symbols\)"),
8514+
("C", r"'C' is not a valid format string \('C' must be followed by a number\)"),
8515+
(".C", r"'.C' is not a valid format string \('C' must be followed by a number\)"),
85148516
))
85158517
@pytest.mark.parametrize("data", [None, {"string": range(3)}])
85168518
def test_plot_format_errors(fmt, match, data):
@@ -8543,6 +8545,11 @@ def test_plot_format():
85438545
line = ax.plot([1, 2, 3], 'k3')
85448546
assert line[0].get_marker() == '3'
85458547
assert line[0].get_color() == 'k'
8548+
fig, ax = plt.subplots()
8549+
line = ax.plot([1, 2, 3], '.C12:')
8550+
assert line[0].get_marker() == '.'
8551+
assert line[0].get_color() == mcolors.to_rgba('C12')
8552+
assert line[0].get_linestyle() == ':'
85468553

85478554

85488555
def test_automatic_legend():

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy